Add SQLAlchemy support for get_or_create()
See original GitHub issueI’m not sure this should be opened as an issue, but I can’t figure out how to do it…
Is there any way to re-use existing records with factory_boy, if they already exist? I can’t find anything in the documentation.
Here’s my scenario:
I’ve got an AddressFactory
for my Address
model. Every Address
has an associated Country
. I’d like to have my AddressFactory
create the Country
if it doesn’t exist, and re-use it if it does.
I know that I can create a Country
manually, and pass it to the AddressFactory
to be used. This works ok for a first-level nesting, but it gets complicated when there’s e.g. a User
who has an Address
which has a Country
. I’d like to avoid having to pass it all the way down the chain.
For most fields, I get around this problem by using sequences to ensure unique names, etc. In this case, I’d really like to re-use existing countries.
I’ve tried to come up with a way to do this dynamically using lazy attributes, but haven’t had any luck so far.
Thanks!
Issue Analytics
- State:
- Created 10 years ago
- Reactions:3
- Comments:11 (7 by maintainers)
Top GitHub Comments
This has finally been added last year 😉
Another one SqlAlchemy avoid duplication implementation.