i'm facing an issue with defaults values of tables.
for example i have this model:
class model1(models.Model):
field1 = models.CharField(max_length=50, default='My Default Value 1',db_column='field1')
field2 = models.CharField(max_length=10)
class Meta:
db_table = 'model1'
and the postgresql table is generated without the default:
mydb=# \d model1
Table "public.model1"
Column | Type | Modifiers
--------+-----------------------+-----------------------------------------------------
id | integer | not null default nextval('model1_id_seq'::regclass)
field1 | character varying(50) | not null
field2 | character varying(10) | not null
Indexes:
"model1_pkey" PRIMARY KEY, btree (id)
the output of migrate sqlmigrate is:
CREATE TABLE "model1" ("id" serial NOT NULL PRIMARY KEY, "field1" varchar(50) NOT NULL, "field2" varchar(10) NOT NULL);
COMMIT;
but if i modify the default value in the model and run a makemigrations/migrate and after that i look into sqlmigrations the output genereates a weird DROP DEFAULT after creating
ALTER TABLE "model1" ALTER COLUMN "field1" SET DEFAULT 'My Default Value 1 modified';
ALTER TABLE "model1" ALTER COLUMN "field1" DROP DEFAULT;
COMMIT;
is there something I am missing or default values for columns are just not supported?
I know it's a bit late, but I was having the same issue. After searching extensively, i found this
"Django uses database defaults to set values on existing rows in a table. It doesn't leave the default values in the database so dropping the default is correct behavior."
https://code.djangoproject.com/ticket/28000
so it looks like this is simply how django has been designed. It bugged me, because when I then inserted data using MySQL, I was given warnings about having no default.
Sigh, I'm sure smarter people than me could come up with a good explanation as to why they've designed it like this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With