question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add a way to fetch a model or create one if it doesn't exist

See original GitHub issue

Migrated issue, originally created by Markus Meskanen (@Mahi)

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()

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:19

github_iconTop GitHub Comments

1reaction
sqlalchemy-botcommented, Nov 27, 2018

Michael Bayer (@zzzeek) wrote:

get_or_create() as being proposed (and as in the blog post EDIT: the very last example accommodates it, at great complexity) does not solve for race conditions. A conflicting row can easily be inserted in between the time that we ask for the row vs. add one to the Session ourselves, causing an INSERT conflict (assuming proper constraints exist, silent data corruption if not). Solving for a race like this can be done in many different ways which are highly dependent on the database and scenario in use - such as using “select…for update” [edit: sorry, keep jumbling up UPDATE/INSERT w/ these issues, for pessimistic this needs to be like LOCK TABLE or serializable isolation], using Postgresql’s “INSERT…ON DUPLICATE UPDATE” is another (works only on Postgresql though).

if django claims their “get_or_create()” solves for race conditions and isn’t trying to do some sort of locking or optimistic concurrency checking, then the irony would be that this bug report claiming django has “solved” this problem is in fact uncovering a bug in django 😃.

I think people here have to appreciate what a big deal “get_or_create()” in the entirely generalized sense really is. People’s applications will work much better if they produce this function themselves since they will be able to tailor for and know exactly what limitations they’ve chosen (since there will have to be some kind of limitations).

0reactions
sqlalchemy-botcommented, Nov 27, 2018

Changes by Michael Bayer (@zzzeek):

  • added labels: wontfix
  • changed status to closed
Read more comments on GitHub >

github_iconTop Results From Across the Web

django - Create if doesn't exist - Stack Overflow
It can be achieved using Model.objects.get_or_create() ... It returns a tuple, if an object is found, get_or_create() returns a tuple of that object...
Read more >
Is it bad coding practice to create something in a get if it does ...
Yes, it matters. In my opinion, it's a generally a bad practice to create something in a procedure that is not documented as...
Read more >
How can I create a directory if it does not exist using Python?
In Python, use the os.path.exists() method to see if a directory already exists, and then use the os.makedirs() method to create it. The...
Read more >
django create if not exists | The AI Search Engine You Control
5. It can be achieved using Model.objects.get_or_create () Example. obj, created = Person.objects.get_or_create ( first_name='John', last_name=' ...
Read more >
Validate PowerShell to Check if a File Exists (Examples)
Learn how to use PowerShell to check if a file exists with Test-Path, Get-Item and even .NET in this in depth guide!
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found