Alex Laban
Alex Laban

Reputation: 59

django, CharField default value is ignored in SQL

I have defined django dummy model as following:

class Test(models.Model):
    test = CharField(max_length = 100, default = 'Test!')

it results into the following SQL

CREATE TABLE `uman_test` (
    `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
    `test` varchar(100) NOT NULL
);

but I expect the default value in SQL also:

CREATE TABLE `uman_test` (
    `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
    `test` varchar(100) NOT NULL DEFAULT 'Test!'
);

why does django model creator ignore the default value?

Thank you!

Upvotes: 2

Views: 1638

Answers (1)

Lachezar
Lachezar

Reputation: 6703

The default value of the field is not in the SQL schema because Django's internals will take care of it - not the database. Before saving the model object, Django will take care to set the default value for this field.

From the docs: "The default value for the field. This can be a value or a callable object. If callable it will be called every time a new object is created."

You can have a callable (a function) as a default value and this can not be represented in the SQL schema.

Upvotes: 8

Related Questions