sil-ai / aqua-api

API for Augmented Quality Assessment
MIT License
3 stars 0 forks source link

Look into keeping one set of models for ORM and pydantic #213

Open LOHMgit opened 1 year ago

LOHMgit commented 1 year ago

ChatGPT's take:

Creating Pydantic models from Tortoise ORM models can be convenient because it allows you to reuse the same class definitions and avoid duplicating code. Additionally, Pydantic models provide additional functionality such as data validation and serialization, which can be useful if you need to interact with data from other systems or APIs.

However, there are also some potential downsides to using Pydantic models directly with Tortoise ORM. For example, Pydantic models may not always have a one-to-one mapping with your database schema, which can cause some complications when querying data. Additionally, there may be performance considerations when using Pydantic models for large datasets, as they can be slower than using plain Tortoise ORM models.

On the other hand, keeping separate models can provide better separation of concerns and make it easier to maintain a clear separation between your application's business logic and its database interactions. This can be especially useful in larger projects or teams where different developers are responsible for different parts of the application.

Ultimately, the decision of whether to use Pydantic models directly with Tortoise ORM or to keep separate models will depend on the specific requirements and constraints of your project.

woodwardmw commented 1 year ago

@LOHMgit Can you show me an example of where we have different models, that might benefit from being aligned?

LOHMgit commented 1 year ago

@woodwardmw Will send you something if I find it. This came out of a different project where I had both

LOHMgit commented 1 year ago

@woodwardmw Looking at the way you have constructed queries in query.py, I don't think you need an orm like tortoise. The example I was working on goes something like this:

_from gql import gql from pydantic import BaseModel

class User(BaseModel): id: int name: str email: str

def generate_query_from_model(model: type) -> str:

Create a list of fields to select in the query

fields = []
for field in model.__fields__.keys():
    fields.append(field)from gql import gql

from pydantic import BaseModel

class User(BaseModel): id: int name: str email: str

def generate_query_from_model(model: type) -> str:

Create a list of fields to select in the query

fields = []
for field in model.__fields__.keys():
    fields.append(field)

# Generate the GraphQL query string
query = f"query {{ {model.__name__.lower()}s {{ {' '.join(fields)} }} }}"

return query

Example usage

query_string = generate_query_from_model(User) query = gql(query_string)

print(query)

# Generate the GraphQL query string
query = f"query {{ {model.__name__.lower()}s {{ {' '.join(fields)} }} }}"

return query

Example usage

query_string = generate_query_from_model(User) query = gql(query_string)

print(query)_

However, this code works seems like more work than it is worth. I think maybe adding a pytest that checks if the graphql queries all match pydantic models would be better