You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using SQLAlchemyModelFactory, if a foreign key to an existing database record is provided, the FK is ignored, a new record is created via the SubFactory, and the provided FK is overwritten with the FK of the record created via the SubFactory.
To Reproduce
Using the below factories and models, AddressFactory() works as expected, creating a new Address and a Person via the SubFactory and committing them to the db.
However, AddressFactory(person_id=1), where 1 is the id of a person that already exists in the db, does not work as I was expecting it to. I'm thinking that in this case, an Address should be created with a foreign key to the existing Person. Instead, a new Address is created, a new Person is created via the SubFactory, and the Address is set to use the new Person.
It is kinda related to #463 and #69; however, in your declaration, there is no way for factory_boy to know that a relation exists between person and person_id.
When you call AddressFactory(person_id=1), you end up with:
If you want some kind of get_or_create behaviour, we'd need to add it to SQLAlchemyFactory; see [the code in DjangoModelFactoryfor inspiration](https://github.com/FactoryBoy/factory_boy/blob/master/factory/django.py#L135-L166). If you did add this toSQLAlchemyFactory(or a custom subclass of it) and declared theidfield to be part of saidget_or_create``, you would get the expected behaviour :-)
sqlalchemy_get_or_create was implemented in 59ee658 and is now being superseded by #794. Feel free to reopen an issue if you think the original concern was not resolved.
Description
When using
SQLAlchemyModelFactory
, if a foreign key to an existing database record is provided, the FK is ignored, a new record is created via the SubFactory, and the provided FK is overwritten with the FK of the record created via the SubFactory.To Reproduce
Using the below factories and models,
AddressFactory()
works as expected, creating a newAddress
and aPerson
via the SubFactory and committing them to the db.However,
AddressFactory(person_id=1)
, where 1 is the id of a person that already exists in the db, does not work as I was expecting it to. I'm thinking that in this case, an Address should be created with a foreign key to the existing Person. Instead, a new Address is created, a new Person is created via the SubFactory, and the Address is set to use the new Person.Model / Factory code
Notes
Possibly related to #463?
The text was updated successfully, but these errors were encountered: