Using Django and MySQL. I save the model and no id appears. Seen some similar issues with PostgreSQL and with bigintegerfields, but neither of those seem to apply here. Any ideas? The client does receive a primary key in the id field in the database via MySQL auto-increment.
Thanks!
class Client(models.Model):
id = models.IntegerField(primary_key=True)
first_name = models.TextField(null=True, blank=True)
last_name = models.TextField(blank=True)
>>> client = models.Client(last_name="Last", first_name="First")
>>> client.last_name
'Last'
>>> client.save()
>>> client.id
>>> client.last_name
'Last'
>>> client.id
>>> client.pk
>>>
And in the database:
id first_name last_name
------------------------------------------
14 First Last
As Rebus says in the comments, you don’t need to define the primary key explicitly. But if you do, you must make sure it is an autoincrement field – ie
AutoField– not a basicIntegerFieldas you have. The way you have it, there’s no way to get a new ID, which is why it’s blank on save.