GorillaPatch
GorillaPatch

Reputation: 5057

Migration of Django app with south leaves default value on column

I have added a new field to my model to track the individual objects to users.

I then generated a migration using south and it asked me for a default value for this migration as the user field has no default value. I entered 1 as the id of the user. I am using sqlite as the db backend for development. Now when I open an sqlite shell and look at the db schema, the default value is still there:

CREATE TABLE "base_category" ("user_id" integer NOT NULL DEFAULT 1,
                              "id" integer PRIMARY KEY,
                              "name" varchar(80) NOT NULL);

However in the migration file it looks like this:

# Adding field 'TimeSlot.user'
db.add_column('base_timeslot', 'user',
                  self.gf('django.db.models.fields.related.ForeignKey')
                  (default=1, to=orm['auth.User']),
                  keep_default=False)

If I understood the south docs correctly, it should not be there:

If keep_default is True, then any default value specified on the field will be added to the database schema for that column permanently. If not, then the default is only used when adding the column, and then dropped afterwards.

In other questions here on SO it was pointed out, that the default values are not handled on a db level, but in python instead.

finally my questions:

  1. Do I have to bother with the default value still in my db, as I do not need it anymore?
  2. Could it be an issue with sqlite, because I was not able to find out how to remove the default value manually using a SQL prompt?

Upvotes: 1

Views: 967

Answers (2)

Shai Berger
Shai Berger

Reputation: 3083

It wasn't considered a bug, but is now; South 0.7.7 (forthcoming as I write this) should fix it.

Upvotes: 2

Scillon
Scillon

Reputation: 320

  1. Do not worry, the DEFAULT 1 in the DB only specifies that if you create a new instance in the table, this field will be populated with 1 by default, but this value can be changed. What keep_default does is disabling the value from being edited, e.g. a new field will get value 1 as default and you can't change the value. This is not your case. You will get default value 1 on new instances but you will be able to edit the value.

  2. Continuing point num 1 - no need to remove the value since DEFAULT 1 is the correct representation of a default value for a new instance of a field.

Upvotes: 0

Related Questions