[Django] #35163: sqlite3.OperationalError: no such column: django_migrations.id

27 views
Skip to first unread message

Django

unread,
Feb 3, 2024, 12:53:59 PM2/3/24
to django-...@googlegroups.com
#35163: sqlite3.OperationalError: no such column: django_migrations.id
-------------------------------------+-------------------------------------
Reporter: milahu | Owner: nobody
Type: Bug | Status: new
Component: Database | Version: 5.0
layer (models, ORM) |
Severity: Normal | Keywords: sqlite
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
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
--
Ticket URL: <https://code.djangoproject.com/ticket/35163>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Feb 3, 2024, 1:04:24 PM2/3/24
to django-...@googlegroups.com
#35163: sqlite3.OperationalError: no such column: django_migrations.id
-------------------------------------+-------------------------------------
Reporter: milahu | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 5.0
(models, ORM) | Resolution:
Severity: Normal | worksforme
Keywords: sqlite | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* resolution: => worksforme

Comment:

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.
--
Ticket URL: <https://code.djangoproject.com/ticket/35163#comment:1>

Django

unread,
Feb 3, 2024, 3:55:48 PM2/3/24
to django-...@googlegroups.com
#35163: sqlite3.OperationalError: no such column: django_migrations.id
-------------------------------------+-------------------------------------
Reporter: milahu | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 5.0
(models, ORM) | Resolution:
Severity: Normal | worksforme
Keywords: sqlite | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by milahu):

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
--
Ticket URL: <https://code.djangoproject.com/ticket/35163#comment:2>

Django

unread,
Feb 3, 2024, 5:36:23 PM2/3/24
to django-...@googlegroups.com
#35163: sqlite3.OperationalError: no such column: django_migrations.id
-------------------------------------+-------------------------------------
Reporter: milahu | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 5.0
(models, ORM) | Resolution:
Severity: Normal | worksforme
Keywords: sqlite | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Tim Graham):

I think we need a more minimal example. Perhaps if you try to put one
together you'll find the cause of the issue.
--
Ticket URL: <https://code.djangoproject.com/ticket/35163#comment:3>

Django

unread,
Feb 3, 2024, 5:41:47 PM2/3/24
to django-...@googlegroups.com
#35163: sqlite3.OperationalError: no such column: django_migrations.id
-------------------------------------+-------------------------------------
Reporter: milahu | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 5.0
(models, ORM) | Resolution:
Severity: Normal | worksforme
Keywords: sqlite | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by milahu):

> 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)
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/35163#comment:4>

Django

unread,
Feb 4, 2024, 4:17:13 AM2/4/24
to django-...@googlegroups.com
#35163: sqlite3.OperationalError: no such column: django_migrations.id
-------------------------------------+-------------------------------------
Reporter: milahu | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 5.0
(models, ORM) | Resolution:
Severity: Normal | worksforme
Keywords: sqlite | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by milahu):

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
--
Ticket URL: <https://code.djangoproject.com/ticket/35163#comment:5>

Django

unread,
Feb 4, 2024, 12:03:07 PM2/4/24
to django-...@googlegroups.com
#35163: sqlite3.OperationalError: no such column: django_migrations.id
-------------------------------------+-------------------------------------
Reporter: milahu | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 5.0
(models, ORM) | Resolution:
Severity: Normal | worksforme
Keywords: sqlite | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Simon Charette):

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
[https://code.djangoproject.com/ticket/32577 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.
--
Ticket URL: <https://code.djangoproject.com/ticket/35163#comment:6>

Reply all
Reply to author
Forward
0 new messages