Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Api for upserting #798

Closed
wants to merge 5 commits into from
Closed

Conversation

sinisaos
Copy link
Member

Related to #252

@codecov-commenter
Copy link

codecov-commenter commented Mar 31, 2023

Codecov Report

Merging #798 (3b566ea) into master (56669fa) will increase coverage by 0.02%.
The diff coverage is 100.00%.

📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

@@            Coverage Diff             @@
##           master     #798      +/-   ##
==========================================
+ Coverage   91.35%   91.37%   +0.02%     
==========================================
  Files         108      108              
  Lines        7517     7538      +21     
==========================================
+ Hits         6867     6888      +21     
  Misses        650      650              
Impacted Files Coverage Δ
piccolo/query/methods/insert.py 97.91% <100.00%> (+0.69%) ⬆️
piccolo/query/mixins.py 94.75% <100.00%> (+0.16%) ⬆️
piccolo/table.py 96.40% <100.00%> (+0.01%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@dantownsend
Copy link
Member

Thanks for this, and the docs.

@sinisaos
Copy link
Member Author

sinisaos commented Apr 1, 2023

No problem. It would be good if you have time to try and review this because if you merge this, it would also solve the problem with duplicate entries in the M2M, because then we will be able to add the on_conflict parameter to the unsaved rows insert query like this

if unsaved:
  await rows[0].__class__.insert(
      *unsaved, on_conflict=OnConflict.do_nothing
  ).run()

): # pragma: no cover
if self.on_conflict_delegate._on_conflict == OnConflict.do_nothing:
base = f'INSERT OR IGNORE INTO "{self.table._meta.tablename}"'
else:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would explicitly check this against OnConflict.do_update, and have the else clause raise a ValueError.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need clarification. Which ValueError should I raise because the user only has two valid options (nothing or update) and if the user enters a wrong value for on_conflict the AttributeError is already raised? If no on_conflict clause is specified, the standard insert query is executed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The value is meant to be an enum but because it's Python could be anything if the user set it to say a string by accident.

if self.on_conflict_delegate._on_conflict == OnConflict.do_nothing:
    base = f'INSERT OR IGNORE INTO "{self.table._meta.tablename}"'
elif self.on_conflict_delegate._on_conflict == OnConflict.do_update:
    ...
else:
    raise ValueError('Invalid on conflict value')

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. I will change that.

@@ -36,6 +51,10 @@ def returning(self: Self, *columns: Column) -> Self:
self.returning_delegate.returning(columns)
return self

def on_conflict(self: Self, conflict: OnConflict) -> Self:
Copy link
Member

@dantownsend dantownsend Apr 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we should allow the user to pass in specific columns?

await Band.insert(Band(name="Pythonistas")).on_conflict(Band.name, do_nothing=True)

If not specified, then we default to all columns.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you look at the tests, even now the user can update one or more columns on do_update conflict. For do_nothing conflict I don't see point of allowing specifying columns because on do_nothing conflict columns don't change anyway.

self, table: t.Type[TableInstance], *instances: TableInstance, **kwargs
self,
table: t.Type[TableInstance],
on_conflict: t.Optional[OnConflict] = None,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would drop consider dropping this, and just using the on_conflict method instead.

The reason being, we can add extra arguments to the on_conflict method in the future, but we don't want to add too many to __init__.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should change it, but feel free to do as you think is best. With this method, we can also easily solve the problem with duplicate entries in M2M. I'm sorry if I didn't understand well what you wanted to say and feel free to change this if you think on_conflict as method would be better way.

@dantownsend dantownsend mentioned this pull request Apr 28, 2023
3 tasks
@sinisaos
Copy link
Member Author

sinisaos commented May 1, 2023

Implemented in #816

@sinisaos sinisaos closed this May 1, 2023
@sinisaos sinisaos deleted the api_for_upserting branch May 1, 2023 07:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants