Opened 10 months ago

Closed 10 months ago

Last modified 10 months ago

#35163 closed Bug (worksforme)

sqlite3.OperationalError: no such column: django_migrations.id

Reported by: milahu Owned by: nobody
Component: Database layer (models, ORM) Version: 5.0
Severity: Normal Keywords: sqlite
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

continue https://github.com/django/django/pull/17815

the error is caused by a django app, trying to record an applied database migration
django/db/migrations/executor.py

workaround: add the "id" column explicitly to class Migration

django/db/migrations/recorder.py

            class Migration(models.Model):
                id = models.IntegerField(primary_key=True)
                app = models.CharField(max_length=255)
                name = models.CharField(max_length=255)
                applied = models.DateTimeField(default=now)

https://github.com/django/django/pull/17815#issuecomment-1925345512

Implicit id is added automatically to all models

but that is not reflected in the sqlite schema: there is no "id" column

so maybe this is a bug in the sqlite backend

Change History (6)

comment:1 by Mariusz Felisiak, 10 months ago

Resolution: worksforme
Status: newclosed

Implicit id is added automatically to all models. I don't think you've explained the issue in enough detail to confirm a bug in Django. Please reopen the ticket if you can debug your issue and provide details about why and where Django is at fault. If you're having trouble understanding how Django works, see TicketClosingReasons/UseSupportChannels for ways to get help.

comment:2 by milahu, 10 months ago

to reproduce, build https://github.com/milahu/archivebox/tree/django4 with django 4.2.9 and run

$ archivebox init
$ sqlite3 index.sqlite3 .schema
CREATE TABLE IF NOT EXISTS "django_migrations" ("app" varchar(255) NOT NULL, "name" varchar(255) NOT NULL, "applied" datetime NOT NULL);

expected: there should be an "id" column in the "django_migrations" table

comment:3 by Tim Graham, 10 months ago

I think we need a more minimal example. Perhaps if you try to put one together you'll find the cause of the issue.

comment:4 by milahu, 10 months ago

to reproduce, build ​https://github.com/milahu/archivebox/tree/django4 with django 4.2.9 and run

the "no such column: id" error seems to be caused by

https://github.com/milahu/archivebox/commit/2e9a1f473ed0abaa9618d6ed6f55aa9202d7acbb

based on https://code.djangoproject.com/ticket/32577

DEFAULT_AUTO_FIELD = 'django.db.models.UUIDAutoField'
class UUIDAutoField(models.fields.AutoField, models.fields.UUIDField):

    def __init__(self, *args, **kwargs):
        #kwargs['db_default'] = RandomUUID()
        #kwargs['db_default'] = uuid.uuid4()
        super().__init__(*args, **kwargs)

    def deconstruct(self):
        name, path, args, kwargs = super().deconstruct()
        #del kwargs['db_default']
        return name, path, args, kwargs

    def get_internal_type(self):
        return 'UUIDAutoField'

    def rel_db_type(self, connection):
        return models.fields.UUIDField().db_type(connection=connection)

models.UUIDAutoField = UUIDAutoField

because later, i get the same "no such column: id" error, when django is trying to migrate this SQL table

class Snapshot(models.Model):

    # FIXME sqlite3.OperationalError: table new__core_snapshot has no column named id
    id = models.UUIDAutoField(primary_key=True, default=uuid.uuid4, editable=False)

comment:5 by milahu, 10 months ago

the "id" columns are missing in the "CREATE TABLE" queries, but later appear in "INSERT INTO" queries

currently im trying to trace the "CREATE TABLE" queries, but these queries are not passed through

django/db/backends/sqlite3/base.py

class SQLiteCursorWrapper(Database.Cursor):
    def executescript(self, query):
        print(f"django/db/backends/sqlite3/base.py executescript query={repr(query)}")
        return super().executescript(query)
    def execute(self, query, params=None):
        # ...
        print(f"django/db/backends/sqlite3/base.py execute query={repr(query)} params={repr(params)}")
        return super().execute(query, params)
    def executemany(self, query, param_list):
        # ...
        print(f"django/db/backends/sqlite3/base.py executemany query={repr(query)} param_list={repr(param_list)}")
        return super().executemany(query, param_list)

the first query i see is

CREATE UNIQUE INDEX "django_content_type_app_label_model_76bd3d3b_uniq" ON "django_content_type" ("app_label", "model")

but before that query i already have the schema

CREATE TABLE "django_migrations" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "app" varchar(255) NOT NULL, "name" varchar(255) NOT NULL, "applied" da
tetime NOT NULL)
CREATE TABLE sqlite_sequence(name,seq)
CREATE TABLE "django_content_type" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "name" varchar(100) NOT NULL, "app_label" varchar(100) NOT NULL, "mod

as reported by

        if True:
            print("schema:")
            for name, sql in super().execute("SELECT name, sql FROM sqlite_schema"):
                for line in sql.split("\n"):
                    print("  " + line)

ideas...?

im using the sqlite backend

i would expect that all queries are passed through SQLiteCursorWrapper

comment:6 by Simon Charette, 10 months ago

As mentioned previously by a few contributors already, this issue tracker is not a support channel so you won't get answers to you questions.

If you are trying to stitch together code to work around a known limitation of the framework with regards to its lack of support for auto-generated UUID then you are likely to run into issues otherwise the ticket would have likely already be resolved.

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