-
-
Notifications
You must be signed in to change notification settings - Fork 400
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
Bug/Feature: Allow updating primary key of an object #1870
Comments
I believe that you should use user.id = 123
new_user = await user.create(**dict(user)) |
That would work in most cases, but the idea is to be able to change the ID while preserving all the other data, not to delete and re-create. The SQL would look like this: I suppose this could break foreign-key references to the updated object. Also I'm not needing to do this in my code after all, so it's not high priority. At least we should get a nicer error message, which I see @waketzheng is doing. |
i don't think it is valid to update primary keys. For instance, when changing the id of an object, Django creates a copy of the object with the new id. However, I think it's better to raise an error in this case and force the user to actually create a new object if they want to have a duplicate. It is less error prone and seems more correct. |
Makes sense to me. |
Describe the bug
If I do this:
the operation fails with a database error:
To Reproduce
See above.
Expected behavior
The object's primary key should be updated.
Additional context
This if-statement
tortoise-orm/tortoise/backends/base/executor.py
Line 270 in 960b1c1
removes the
id
from the update field list, and this results in an empty SQL statement being generated.I can update the primary key with hand-written SQL just fine.
The text was updated successfully, but these errors were encountered: