8👍
✅
Try explicitly specifying the id
field and marking it as the primary key:
class UserProfile(models.Model):
id = models.BigIntegerField(primary_key = True)
user = models.OneToOneField(User)
avatar = models.ImageField(blank=True, upload_to=get_image_path, default='/static/image/avatar/male.png')
age = models.IntegerField(default=4, validators=[MinValueValidator(3), MaxValueValidator(99)])
Django should automatically create a sequence for this field.
It may be that the User
foreign key without an explicitly defined primary key is confusing the ORM, although that’s just a theory.
1👍
If you are developing locally, and don’t care about your migration history, and you just need a quick-fix, you could do this:
manage.py migrate <your app name> zero
Then delete the migration files except for the __init__.py
in <your app name>
.
Finally:
manage.py makemigrations <your app name>
manage.py migrate
A more complicated approach would be learning how to write and modify migration files to populate existing rows.
- Confused by Django's claim to MVC, what is it exactly?
- Implementing HATEOAS in Django REST framework
- How to cache a model method in django?
- Trying to serve django static files on development server – not found
- Is there a Django ModelField that allows for multiple choices, aside from ManyToMany?
Source:stackexchange.com