-
-
Notifications
You must be signed in to change notification settings - Fork 452
fix: update pk field only raises unfriendly error #1873
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
Changes from 4 commits
94f9c69
77986e5
8264369
4a1a39c
0a88a96
62d63e9
981d835
ce75192
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -267,13 +267,15 @@ async def execute_update( | |
| values = [] | ||
| expressions = {} | ||
| for field in update_fields or self.model._meta.fields_db_projection.keys(): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current logic assumes that if no values and no expressions, then the user must have been passed pk as part of for field in update_fields or self.model._meta.fields_db_projection.keys():
field_obj = self.model._meta.fields_map[field]
if field_obj.pk:
if update_fields:
raise OperationalError(
f"Can't update pk field, use `{self.model.__name__}.create` instead."
)
continue
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should allow
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Why introduce a behavior that triggers a warning? This doesn't seem like a good design. You usually introduce warnings about deprecation or something you want users to stop doing. |
||
| if not self.model._meta.fields_map[field].pk: | ||
| instance_field = getattr(instance, field) | ||
| if isinstance(instance_field, Expression): | ||
| if not (field_obj := self.model._meta.fields_map[field]).pk: | ||
waketzheng marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if isinstance(instance_field := getattr(instance, field), Expression): | ||
| expressions[field] = instance_field | ||
| else: | ||
| value = self.model._meta.fields_map[field].to_db_value(instance_field, instance) | ||
| values.append(value) | ||
| values.append(field_obj.to_db_value(instance_field, instance)) | ||
| if not values and not expressions: | ||
| raise OperationalError( | ||
| f"Can't update pk field only, use `{self.model.__name__}.create` instead." | ||
| ) | ||
| values.append(self.model._meta.pk.to_db_value(instance.pk, instance)) | ||
| return ( | ||
| await self.db.execute_query(self.get_update_sql(update_fields, expressions), values) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.