Saving a model with a custom primary key
See original GitHub issueHi, I’m not sure if this is an issue or I’m doing something wrong but I picked tortoise for a new asyncio project (previously have been using peewee) and have encountered 1 issue.
I have a model such as:
class User(Model):
id = fields.BigIntField(pk=True)
name = fields.CharField(max_length=50)
created_at = fields.DatetimeField(auto_now_add=True)
class Meta:
table = "users"
When I create an instance of it, doing something like:
user = User(
id = 34553535,
name = 'username'
)
await user.save()
There’s no error/exception/or any feedback, and the record is not inserted into the database. I know with peewee I’d have to do something like .save(force_insert=True)
- but didn’t see anything in the tortoise documentation that would achieve a similar result.
Also, is there a way to tell if the save() is successful? With peewee I’d be able do something like
if user.save() > 0:
print("User saved successfully")
else:
print("User did not save")
Thanks for the work on this plugin, I’ve been looking for an asyncio ORM and was happy to find one under such active development.
Issue Analytics
- State:
- Created 4 years ago
- Comments:8 (3 by maintainers)
Top Results From Across the Web
Saving a model with one more key than just id - Laracasts
Making a custom function is possible and works, but doesn't this use more resources DB wise than skipping the composite keys and simply...
Read more >Django 3 - Model.save() when providing a default for the ...
As I understand, if we try to call Model.save() , it would always create a new record instead of updating if the model...
Read more >Custom Django model field based on default Primary Key
It first saves the instance normally and let the database fills in the primary key id for us. This step is necessary if...
Read more >Models - Django documentation
Each model is a Python class that subclasses django.db.models. ... If you'd like to specify a custom primary key, specify primary_key=True on one...
Read more >Model instance reference — Django 4.1.4 documentation
This method should be used to provide custom model validation, ... Specifically, when you call save() and the object's primary key attribute ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Just tried 0.15.0 and it raised the exception, going to close the issue.
I think raising a
ValueError
when trying to override a generated field is good. I’m ok with the current behavior otherwise.Thanks again, really enjoying tortoise so far.