Skip to content

Fix SQLite AUTOINCREMENT constraint ordering#7664

Open
russellromney wants to merge 1 commit into
tobymao:mainfrom
russellromney:codex/sqlite-autoincrement-order
Open

Fix SQLite AUTOINCREMENT constraint ordering#7664
russellromney wants to merge 1 commit into
tobymao:mainfrom
russellromney:codex/sqlite-autoincrement-order

Conversation

@russellromney
Copy link
Copy Markdown

@russellromney russellromney commented May 20, 2026

Summary

Fix SQLite generation for MySQL auto-incrementing primary keys so AUTOINCREMENT is preserved regardless of source constraint order and emitted in a SQLite-valid position.

Before this change, semantically equivalent MySQL definitions produced different SQLite SQL:

-- input
CREATE TABLE z (id INT AUTO_INCREMENT PRIMARY KEY)
-- previous SQLite output
CREATE TABLE z (id INTEGER PRIMARY KEY)

-- input
CREATE TABLE z (id INT PRIMARY KEY AUTO_INCREMENT)
-- previous SQLite output
CREATE TABLE z (id INTEGER PRIMARY KEY AUTOINCREMENT)

This also fixes a table-level primary key case that previously generated SQLite syntax rejected by SQLite itself:

-- previous SQLite output
CREATE TABLE x (id INTEGER NOT NULL AUTOINCREMENT PRIMARY KEY)
-- fixed SQLite output
CREATE TABLE x (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT)

SQLite accepts AUTOINCREMENT after INTEGER PRIMARY KEY; placing it before PRIMARY KEY is a syntax error.

Changes

  • Normalize SQLite column constraints so AutoIncrementColumnConstraint follows the column PrimaryKeyColumnConstraint when both are present.
  • Preserve AUTOINCREMENT for AUTO_INCREMENT PRIMARY KEY as well as PRIMARY KEY AUTO_INCREMENT.
  • Update MySQL and SQLite dialect tests to cover both source orders and the valid table-level primary key output.

Tests

python3 -m pytest tests/dialects/test_sqlite.py -q
python3 -m pytest tests/dialects/test_mysql.py -q

I also sanity-checked the fixed SQLite output with Python's sqlite3 module.

@russellromney russellromney force-pushed the codex/sqlite-autoincrement-order branch from 26c60e5 to fc7f4de Compare May 20, 2026 22:16
Copy link
Copy Markdown
Collaborator

@VaggelisD VaggelisD left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @russellromney, thank you for the PR! Just a couple of nits:

Comment thread tests/dialects/test_sqlite.py Outdated
"sqlite": "CREATE TABLE z (a INTEGER PRIMARY KEY AUTOINCREMENT)",
"mysql": "CREATE TABLE z (a INT PRIMARY KEY AUTO_INCREMENT)",
},
)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test seems like a duplicate of the other, I think I'd wrap it in a for loop e.g

for constraint in ("PRIMARY KEY AUTO_INCREMENT", "AUTO_INCREMENT PRIMARY KEY"):
  self.validate_all(
    ...,
    read={
      "mysql": f"CREATE TABLE z (a INT {constraint})",
   },
   ...
 )

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding to this^, make sure to do with self.subTest if you do the loop.

Comment thread sqlglot/generators/sqlite.py Outdated
return expression


def _ensure_auto_increment_follows_primary_key(column: exp.ColumnDef) -> None:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the previous for loop was a bit easier to understand plus we also don't have to loop over constraints twice with next; Should we revert and patch it with insert instead?

Comment thread tests/dialects/test_mysql.py
@russellromney russellromney force-pushed the codex/sqlite-autoincrement-order branch from fc7f4de to c52b36b Compare May 22, 2026 22:34
@russellromney
Copy link
Copy Markdown
Author

All feedback addressed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants