#24030: default id field not migrated correctly
----------------------------+--------------------
Reporter: brian | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 1.7
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------+--------------------
In my db models, I had legacy id fields that looked like:
{{{
id = models.AutoField(primary_key=True)
}}}
The relevant migration was:
{{{
migrations.AlterField(
model_name='person',
name='id',
field=models.AutoField(verbose_name='ID', serialize=False,
auto_created=True, primary_key=True),
preserve_default=True,
),
}}}
Which resulted in the following sql:
{{{
ALTER TABLE "spud_person" ALTER COLUMN "id" DROP DEFAULT;
}}}
I removed these fields, and made a new migration, and ran it.
Unfortunately, the id field no longer auto generates.
{{{
Exception Value: null value in column "id" violates not-null constraint
}}}
Non-working table:
{{{
spud=> \dS spud_person;
user_url | character varying(200) | not null
Table "public.spud_person"
Column | Type | Modifiers
----------------+------------------------+-----------
id | integer | not null
}}}
For comparison here is a working table:
{{{
spud=> \dS spud_feedback
Table "public.spud_feedback"
Column | Type |
Modifiers
-----------------+--------------------------+------------------------------------------------------------
id | integer | not null default
nextval('spud_feedback_id_seq'::regclass)
}}}
I suspect this is a postgres specific issue.
If my understanding is correct, when the table is created it is created as
type "serial", which makes it integer with the nextval set, however it is
not possible to set an existing table column to serial.
I believe something like the following is needed to fix the problem:
{{{
create sequence spud_person_id_seq;
ALTER TABLE "spud_person" ALTER COLUMN "id" set DEFAULT
nextval('spud_person_id_seq'::regclass);
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/24030>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/048.69ba29cc5564edb9ad2f976c93bea56c%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.