Opened 10 years ago
Last modified 10 years ago
#23415 closed Bug
OneToField reference to default 'id' instead of actual primary key — at Version 2
Reported by: | sky-chen | Owned by: | nobody |
---|---|---|---|
Component: | Migrations | Version: | 1.7 |
Severity: | Release blocker | Keywords: | |
Cc: | Markus Holtermann | Triage Stage: | Accepted |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
Reproduce
- create a new project 'bug'
- create two app 'deg', 'pubsite'
- create models:
# bug/deg/models.py from django.db import models # Clients class Client(models.Model): client_id = models.IntegerField('ID', primary_key=True) name = models.CharField('Name', max_length=255) short_name = models.CharField('Short name', max_length=35) class Meta: managed = False db_table = 'Clients' verbose_name = 'Client' # bug/pubsite/models.py from django.db import models from django.contrib.auth.models import User from deg.models import Client class Account(models.Model): user = models.OneToOneField(User) deg_client = models.OneToOneField(Client)
- make migrations
- check sqlmigrate pubsite 0001
BEGIN; CREATE TABLE `pubsite_account` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `deg_client_id` integer NOT NULL UNIQUE, `user_id` integer NOT NULL UNIQUE); ALTER TABLE `pubsite_account` ADD CONSTRAINT pubsite_account_deg_client_id_1b7724045f4a7977_fk_Clients_id FOREIGN KEY (`deg_client_id`) REFERENCES `Clients` (`id`); ALTER TABLE `pubsite_account` ADD CONSTRAINT pubsite_account_user_id_3d5e7937e7fad36_fk_auth_user_id FOREIGN KEY (`user_id`) REFERENCES `auth_user` (`id`); COMMIT;
Change History (2)
comment:1 by , 10 years ago
Cc: | added |
---|---|
Triage Stage: | Unreviewed → Accepted |
Type: | Uncategorized → Bug |
comment:2 by , 10 years ago
Description: | modified (diff) |
---|
Note:
See TracTickets
for help on using tickets.
Some details that are causing the problem: The migration
0001
fordeg.Client
has aCreateModel
operation that adds the Client model to Django's migration state. Unfortunately the operation doesn't contain any fields:Therefore Django doesn't know about the explicit primary key field and cannot point the FK their but rather relies on some defaults. The problem is in
django.db.migrations.autodetector.MigrationAutodetector.generate_created_unmanaged()
and.generate_created_proxies()
and which makes proxy models also be affected by that bug.Right now this problem can be worked around with the following steps:
dep
andpubsite
managed = False
tomanaged = True
manage.py makemigrations dep pubsite
managed = True
back tomanaged = False
dep.migrations.0001_initial
add'managed': False
as a new option to theoptions
argument of theCreateModel
operation.