﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
24030	default id field not migrated correctly	Brian May	nobody	"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);
}}}"	Bug	closed	Migrations	1.8	Normal	fixed			Unreviewed	0	0	0	0	0	0
