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.

Can I perform an upsert?

See original GitHub issue

Is it possible to perform an upsert with fireo, and if so, could you provide a quick example of how to do so?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
AxeemHaidercommented, Mar 14, 2020

save() method work as upsert when first you are getting data and then update any field and save it will work as upsert otherwise if you provide same id with any other document then it will override the document.

class Tweet(Model):
    id = IDField()
    tweet = TextField()
    other_field = TextField()


t = Tweet()
t.id = 'id-123'
t.tweet = 'some tweet'
t.other_field = 'any value'
t.save()

# When save() method behave like upsert
t2 = Tweet.collection.get(t.key)
t2.tweet = 'tweet changed'
t2.save()

# When save() method behave like override
# In this case other_field will be None
t3 = Tweet()
t3.id = 'id-123'
t3.tweet = 'tweet changed'
t3.save()

Actually save() always override the document but when you first get() the document change any field and save it other fields are already containing the values so it look like upsert() method. if you print(t2) you will see the value of t2.other_field

t2 = Tweet.collection.get(t.key)
print(t2)
t2.tweet = 'tweet changed'
t2.save()

# This work same as
t2 = Tweet()
t2.id = 'id-123'
t2.tweet = 'some tweet'
t2.other_field = 'any value'
t2.save()
0reactions
jonesnccommented, Mar 11, 2020

I’m not sure an upsert method is required, since the save() method effectively performs an upsert.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Upsert in SQL: What is an upsert, and when should you use ...
The UPSERT command in CockroachDB performs an upsert based on the uniqueness of the primary key column or columns, and it will perform...
Read more >
What is UPSERT and how to do it in MySQL - Linux Hint
You can perform UPSERT using MySQL in three ways. UPSERT using INSERT IGNORE; UPSERT using REPLACE; UPSERT using ON DUPLICATE KEY UPDATE. Before...
Read more >
MySQL UPSERT | INSERT or UPDATE Whichever is Applicable
This tutorial explains about MySQL UPSERT command with the help of simple examples. An upsert is a smart operation which turns into INSERT...
Read more >
Performing UPSERT Operation - Skyvia Overview
The UPSERT operation updates a record if it exists or inserts a new record. This allows you to avoid inserting duplicate data. You...
Read more >
Performing Upserts - SingleStore Documentation
An upsert inserts a row r into a table if the table does not already contain an existing row with the same primary...
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