New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Model not seting default values after building a factory #128
Comments
@eloi-martinez-qida It would help a lot if you provide a minimal example (py file + list of dependencies with their version) that people can run. For example, I have no experience with Since I have no clue how |
@jtraub yes, sorry for missing that, the problem persists using a pydantic BaseModel and makes the example simpler. import datetime
from uuid import uuid4
import ormar
from pydantic import UUID4, BaseModel
from pydantic_factories import OrmarModelFactory
class TestModel(BaseModel):
id: UUID4 = ormar.UUID(primary_key=True, default=uuid4)
text: str = ormar.Text()
created_date: datetime.datetime = ormar.DateTime(default=datetime.datetime.now)
class TestModelFactory(OrmarModelFactory):
__model__ = TestModel
def test_test_model():
TestModelFactory.build()
TestModel(text='qwerty') Versions from pip freeze:
|
The base project is this template with ormar: https://github.com/s3rius/FastAPI-template |
Thanks a lot. I can reproduce the problem now. |
Thanks you for the fast response and pointing out what i was lacking |
@eloi-martinez-qida could you please install pydantic-factories from this branch - https://github.com/starlite-api/pydantic-factories/tree/fix_ormar and check if your actual code still fails. I noticed that for some reason Pydantic sets |
thanks, now id and created_date works as expected, but for some reason, state does not (it's an Enum, sorry for not including it in the example, i was trying to make it as simple as possible) I'm appending an example for you to test: import datetime
from enum import Enum
from uuid import uuid4
import ormar
import sqlalchemy
from databases import Database
from pydantic import UUID4
from pydantic_factories import OrmarModelFactory
class TestState(Enum):
NEW = 'NEW'
OK = 'OK'
ERROR = 'ERROR'
class TestModel(ormar.Model):
class Meta:
database = Database('postgresql://')
metadata = sqlalchemy.MetaData()
id: UUID4 = ormar.UUID(primary_key=True, default=uuid4)
text: str = ormar.Text()
created_date: datetime.datetime = ormar.DateTime(default=datetime.datetime.now)
state: Enum = ormar.Enum(enum_class=TestState, default=TestState.NEW)
class TestModelFactory(OrmarModelFactory):
__model__ = TestModel
def test_test_model():
TestModelFactory.build()
TestModel(text='qwerty') |
@eloi-martinez-qida that is quite interesting behaviour here. I receive |
@jtraub Are you inheriting TestModel from ormar.Model or BaseModel?
When not building the Factory, it runs as expected. |
@jtraub any clue why this could happen? maybe something else needs to be deep_copied? |
@jtraub can you reproduce the bug? |
@eloi-martinez-qida hi, yes. I can reproduce it. Actually, I am looking into it right now. I had something else happening so I was offline for the most of the last week. |
@eloi-martinez-qida could you please check again by installing pydantc-factories from the same branch? |
@jtraub yes, that fixes the enum problem, thanks! Now i have found a problem with nullable fields, sorry for not caching it beforehand I also found some tests that are failing with the presence of other tests (might not be related, i have to look into it more) |
@eloi-martinez-qida do the tests again, please :-) If this won't help I will simply switch to unconditional deep copy. This would impact performance a bit but at least it should work in all cases. |
@jtraub Yes, sorry for the delay, now everything works as intended, thanks and nice job!! |
Creating a model after building a factory of the model rises a validation error of the default values not being defined.
Example:
results in:
Removing TestModelFactory.build() works as intended.
The problem persists if the factory is called from one test and the instancing in the model in another one.
Ex.
The text was updated successfully, but these errors were encountered: