Why isn't SQLAlchemy translating this object generated by a FactoryBoy SubFactory into a foreign key?

送分小仙女□ 提交于 2019-12-05 12:27:47

The problem is that the GearItemFactory definition specifies a Python object reference for the foreign key database ID. sqlalchemy has no problem translating Python objects into database Foreign Key ID. However in my factory I specified an object-to-database column mapping rather than an object-to-object mapping, so SQLAlchemy (rightfully) thinks I want to pass the Python object straight to the database. Just need to change the factory foreign key to an object-to-object mapping and sqlalchemy will handle the actual database FK column behind the scenes.

This is the broken line:

category_id = factory.SubFactory(GearCategoryFactory)

See how the backref on GearCategory is named category not category_id? Updating that line to use category fixes the problem:

category = factory.SubFactory(GearCategoryFactory)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!