-
-
Notifications
You must be signed in to change notification settings - Fork 374
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
Duplicate model instance #417
Comments
My question is, what is that attribute used for? I guess is not that easy to change something like this. I read a little bit the code in order to figure out how it is used and why it is needed, but I did not spend much time and did not understand properly the importance of that field |
Is a model with pk set an update, or a custom create? Pk=None shouldn't change, so we can make it understand that aa expected? |
Sorry I did not understand your queries, can you please rephrase? The model is created with My question is, let's say that I have a model with multiple fields and I want to create a brand new one that is the same, like the following class Book(Model)
author = fields.CharField(max_length=32)
title = fields.CharField(max_length=32)
# other fields and create a copy of a book since they share the same author or other fields for that matter. Let aside the fact that the author can be a foreign key to another model, this is just an example. Wouldn't be easier behaving like |
null could be a valid PK (usually bad design but technically valid) so just setting it to null on an existing object alone is not gona be enough to determine if it should do a create or a update hence the _saved_in_db field, that's how it determines if it should try to create or update the object the simplest solution would probably be a .clone helper that just makes a new instance that isn't saved yet? |
@AEnterprise a But would you encourage bad design? |
By the way, I would very much open to create a PR for a Are you sure, |
is it bad design to have a null key in your database? absolutely. mysql, mariadb, postrgess and most other database systems do not allow this. However sqlite (that tortoise supports) still allows it for backwards compatible reasons so it techincally is a thing: https://sqlite.org/lang_createtable.html i can't think of a good reason to do it but it's a thing so the assumtion of "null"/"None" means that there is no pk set is technically not a valid assumption in all cases as for _saved_in_db: pretty sure that is the case but i didn't design it so i might be missing something i overlooked when working wit it before |
I understand, but this raises a question, how does django handle this? 😂 Are you open for a PR for the I am eager to start contributing to this library that I just started using. |
@AEnterprise Whilst @WisdomPill However, a Both are viable? |
Actually, we will likely have to special case the |
I think an error should be raised like when we are creating a model instance in the "standard" way. About the
|
Ok, so I am busy implementing this, and the way to handle just setting pk to None just doesn't feel right. It can allow for silently swallowing errors based on a guess what the user meant, which is a REALLY BAD THING. We also can't disallow PK being nullable as:
So, I feel that I should only do the |
Well, if you need some help count me in, I am busy at work for a couple of days and I thought on working on it as well on the weekend. Can you please assign this issue to me or yourself so that we do not work on the same thing? I also wanted to work on using asyncio test cases that are built in with python3.8 from #416, can you assign that issue to me please? |
I'm almost done with this, You're welcome to do #416, so I assigned it to you. |
Thanks! |
Do you also think these issues should be added to the 0.17 project? As of now, 0.16 had many releases |
Normally we only bump the major release if it is a large change that people need to be aware of. And yes, I agree that 0.16 had by now too many releases 🤷 I originally planned 0.17 to include a field refactor so we can support oracle syntax without too many hacks. And so we can differentiate between preparing/escaping data for parameter-passing or inline-SQL or for LIKE. Also, turns out that Tortoise doesn't support nullable PK's for a while now. We generate an error, so my previous case is actually a lot simpler to describe. So the val=None could work for cases where we can generate the pk, and it is now much simpler a condition. |
Released 0.16.13 with clone and implicit change 😄 |
Is your feature request related to a problem? Please describe.
Usually in django to duplicate a model and to not copy every field there is a work around that is pretty straightforward and easy to do. This can be done via setting the
pk
toNone
. But with tortoise is not that easy, setting also the attribute._saved_in_db
is necessary.Describe the solution you'd like
Not to access a private variables for this trick to work.
Describe alternatives you've considered
Maybe a copy method as well, that would be much more intuitive.
Additional context
Example of the code needed as of now
Example of the code that I would like to use instead
The text was updated successfully, but these errors were encountered: