"last_login", null=True not migrated

79 views
Skip to first unread message

Norberto Bensa

unread,
Jan 5, 2016, 7:29:21 AM1/5/16
to Django users
Hello,

I have a project originally developed using Django 1.5 but upgraded up to 1.9.1. It uses a custom user model (from AbstractUser).

Yesterday I had to create a user and Django replied with an exception. After some googling, this exception was caused because last_login can now be None while the database schema says last_login is not null.

Django's "makemigrations" detects the change but it is run in the first migration. This migration cannot be applied because the table already exists so I faked it.

Then I made a standalone migration. Django says this migration IS applied but the database says it is not.

Of course I can apply the migration by hand using SQL (or so I hope) but I'd like to use migrations for this task. And so the question. Why doesn't it apply?


Thanks!!
Norberto


===== console output =====

(prueba)ubuntu@consulta:~/prueba⟫ ./manage.py migrate accounts
Operations to perform:
  Apply all migrations: accounts
Running migrations:
  Rendering model states... DONE
  Applying accounts.0002_consultauser_last_login_null... OK


===== postgresql output =====

prueba=> \d accounts_consultauser
                                     Table "public.accounts_consultauser"
    Column    |           Type           |                             Modifiers                              
--------------+--------------------------+--------------------------------------------------------------------
 id           | integer                  | not null default nextval('accounts_consultauser_id_seq'::regclass)
 password     | character varying(128)   | not null
 last_login   | timestamp with time zone | not null
.
.
.

==== migration code ====


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('accounts', '0001_initial'),
    ]

    operations = [
        migrations.AlterField(
            model_name='consultauser',
            name='last_login',
            field=models.DateTimeField(null=True, verbose_name='last login', blank=True),
        ),

    ]



술욱

unread,
Jan 5, 2016, 9:00:05 AM1/5/16
to django...@googlegroups.com
BTW, I just discovered:

$ ./manage.py sqlmigrate accounts 0002
$

In other words, empty.

Bug?




--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/3880a2f5-737c-4232-b02f-563a5c1d93a6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tim Graham

unread,
Jan 5, 2016, 10:19:46 AM1/5/16
to Django users
You initial migration must have a state with the field as null=False so that Django can detect it has changed between migration 1 and 2. It sounds like you may have gotten that incorrect.

술욱

unread,
Jan 5, 2016, 10:49:58 AM1/5/16
to django...@googlegroups.com
Of course! Thanks for pointing it out :-)



Reply all
Reply to author
Forward
0 new messages