question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Saving a model with a custom primary key

See original GitHub issue

Hi, 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:closed
  • Created 4 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
bbedwardcommented, Nov 24, 2019

Just tried 0.15.0 and it raised the exception, going to close the issue.

1reaction
bbedwardcommented, Nov 23, 2019

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found