#21917 closed Bug (fixed)

Breakage in SQLite migrations

Reported by: apollo13 Owned by: Andrew Godwin <andrew@…>
Component: Migrations Version: master
Severity: Release blocker Keywords:
Cc: apollo13, andrewgodwin Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Error:

Applying oversight.0001_logentry_sensor...Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/florian/sources/django.git/django/core/management/__init__.py", line 427, in execute_from_command_line
    utility.execute()
  File "/home/florian/sources/django.git/django/core/management/__init__.py", line 419, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/florian/sources/django.git/django/core/management/base.py", line 287, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/home/florian/sources/django.git/django/core/management/base.py", line 336, in execute
    output = self.handle(*args, **options)
  File "/home/florian/sources/django.git/django/core/management/commands/migrate.py", line 145, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False))
  File "/home/florian/sources/django.git/django/db/migrations/executor.py", line 60, in migrate
    self.apply_migration(migration, fake=fake)
  File "/home/florian/sources/django.git/django/db/migrations/executor.py", line 94, in apply_migration
    migration.apply(project_state, schema_editor)
  File "/home/florian/sources/django.git/django/db/migrations/migration.py", line 97, in apply
    operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
  File "/home/florian/sources/django.git/django/db/migrations/operations/fields.py", line 35, in database_forwards
    field,
  File "/home/florian/sources/django.git/django/db/backends/sqlite3/schema.py", line 95, in add_field
    raise ValueError("You cannot add a null=False column without a default value on SQLite.")
ValueError: You cannot add a null=False column without a default value on SQLite.

Models:

from django.db import models
from django.utils.timezone import now


class Sensor(models.Model):
    name = models.CharField(max_length=255)
    api_endpoint = models.SlugField()
    unit = models.CharField(max_length=255)
    sensor_class = models.CharField(max_length=255)
    params = models.TextField()
    current_log = models.ForeignKey('LogEntry', null=True, related_name='+')

    def __unicode__(self):
        return self.name


class LogEntry(models.Model):
    datetime = models.DateTimeField(default=now)
    sensor = models.ForeignKey(Sensor)
    value = models.CharField(max_length=255)

Migrations generated by makemigrations:

# encoding: utf8
from django.db import models, migrations
import django.utils.timezone


class Migration(migrations.Migration):

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='LogEntry',
            fields=[
                (u'id', models.AutoField(verbose_name=u'ID', serialize=False, auto_created=True, primary_key=True)),
                ('datetime', models.DateTimeField(default=django.utils.timezone.now)),
                ('value', models.CharField(max_length=255)),
            ],
            options={
            },
            bases=(models.Model,),
        ),
        migrations.CreateModel(
            name='Sensor',
            fields=[
                (u'id', models.AutoField(verbose_name=u'ID', serialize=False, auto_created=True, primary_key=True)),
                ('name', models.CharField(max_length=255)),
                ('api_endpoint', models.SlugField()),
                ('unit', models.CharField(max_length=255)),
                ('sensor_class', models.CharField(max_length=255)),
                ('params', models.TextField()),
                ('current_log', models.ForeignKey(to='oversight.LogEntry', to_field=u'id', null=True)),
            ],
            options={
            },
            bases=(models.Model,),
        ),
    ]

and:

# encoding: utf8
from django.db import models, migrations


class Migration(migrations.Migration):

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

    operations = [
        migrations.AddField(
            model_name='logentry',
            name='sensor',
            field=models.ForeignKey(to='oversight.Sensor', to_field=u'id'),
            preserve_default=True,
        ),
    ]

Change History (3)

comment:1 Changed 13 months ago by timo

  • Triage Stage changed from Unreviewed to Accepted

comment:2 Changed 13 months ago by bmispelon

For the record, here's a simplified models.py that reproduce the error:

from django.db import models

class Foo(models.Model):
    bar_fk = models.ForeignKey('Bar', null=True)

class Bar(models.Model):
    foo_fk = models.ForeignKey('Foo')

I'll also note that the same models work fine on 1.6 with syncdb.

comment:3 Changed 13 months ago by Andrew Godwin <andrew@…>

  • Owner set to Andrew Godwin <andrew@…>
  • Resolution set to fixed
  • Status changed from new to closed

In a7e2957110e8cac6ad5bf233ce4cc1fa528ac0e0:

Fixed #21917: Overly cautious SQLite3 backend for null fields + defaults

Note: See TracTickets for help on using tickets.
Back to Top