[Sqlalchemy-tickets] Issue #3942: Add a way to fetch a model or create one if it doesn't exist (zzz
Brought to you by:
zzzeek
From: Markus M. <iss...@bi...> - 2017-03-18 16:43:56
|
New issue 3942: Add a way to fetch a model or create one if it doesn't exist https://bitbucket.org/zzzeek/sqlalchemy/issues/3942/add-a-way-to-fetch-a-model-or-create-one Markus Meskanen: Django has a `get_or_create()` method for times when you don't know if data for an entity already exists. There are multiple implementations available for SQLAlchemy at http://stackoverflow.com/questions/2546207/does-sqlalchemy-have-an-equivalent-of-djangos-get-or-create/, but I thought it would be nice to have a native support for such widely used feature. I'm not sure of the ideal implementation, but one that pleases my eye is to add a keyword arugment for `one()` etc: session.query(Player).filter_by(id=my_player_id).one(create=True) Or simply add a new method: session.query(Player).filter_by(id=my_player_id).get_or_create() |