sqlite DB missed DEFAULT parameter from table definition

35 views
Skip to first unread message

HEMENDRA SINGH HADA

unread,
Aug 6, 2018, 5:19:29 PM8/6/18
to Django users
Hi Team, 

My application using sqlite DB and in my model table class, I have define attributes like Below - 

class MyUser(AbstractBaseUser):
    email = models.EmailField(
        verbose_name='email address',
        max_length=255,
        unique=True,
    )
    date_of_birth = models.DateField()
    is_active = models.BooleanField(default=True)
    is_admin = models.BooleanField(default=False)

    objects = MyUserManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['date_of_birth']


But we I see the same table in sqlite interface, it shows me table definition like this-

sqlite> .dump authapp_myuser
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE "authapp_myuser" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "password" varchar(128) NOT NULL, "last_login" datetime NULL, "email" varchar(255) NOT NULL UNIQUE, "date_of_birth" date NOT NULL, "is_active" bool NOT NULL, "is_admin" bool NOT NULL);
COMMIT;

If I am giving is_active field with default value True, Then why its not showing in "is_active" bool NOT NULL. 
 
This case is working fine when I insert any data from GUI application or Django admin page, If I try to insert something from sqlite interface, I need to pass that default value too. 

Please give some suggestion, Because I have requirment where I need to insert or update the table values from outside Django App.

Christophe Pettus

unread,
Aug 6, 2018, 5:22:55 PM8/6/18
to django...@googlegroups.com

> On Aug 6, 2018, at 10:19, HEMENDRA SINGH HADA <hada.he...@gmail.com> wrote:
>
> If I am giving is_active field with default value True, Then why its not showing in "is_active" bool NOT NULL.

Django "default=" defaults are implemented in Django, not in the database. Django does not currently set database-level defaults.

--
-- Christophe Pettus
x...@thebuild.com

HEMENDRA SINGH HADA

unread,
Aug 6, 2018, 5:27:04 PM8/6/18
to Django users
Than How can I achieve my Goal with sqlite and Django ?? 
Do you have any solution for that ??

Christophe Pettus

unread,
Aug 6, 2018, 5:31:43 PM8/6/18
to django...@googlegroups.com

> On Aug 6, 2018, at 10:27, HEMENDRA SINGH HADA <hada.he...@gmail.com> wrote:
>
> Than How can I achieve my Goal with sqlite and Django ??
> Do you have any solution for that ??

You'll need to either:

1. Add the default to the database using a migration, either using Django's migration framework and the RunSQL migration, or outside of Django, or,
2. Specify the value you want inserted when inserting directly using SQL.
Reply all
Reply to author
Forward
0 new messages