Stop forcing *increments() columns to be the table primary key#7
Merged
Conversation
Ported from MasoniteFramework/orm#950. increments(), tiny_increments() and big_increments() no longer implicitly mark the column as the table's primary key. This aligns them with every other datatype usable as a primary key and unblocks tables that pair a non-integer primary key (e.g. uuid) with an auto-numbered column. - .primary() on an *increments() column compiles the PRIMARY KEY inline through dedicated *_increments_primary type mappings. - id() keeps the previous convenience behavior (big_increments + primary). - The migrations table schema uses increments().primary() explicitly. - SQLite raises QueryException for non-primary *increments() columns since SQLite only supports AUTOINCREMENT on the INTEGER PRIMARY KEY. - Schema honors explicitly provided connection_details instead of always reloading them from the config file. BREAKING: migrations relying on increments() implying the primary key must add .primary() to the column definition to keep the previous behavior. Closes #4
- SQLite: AUTOINCREMENT is only valid on INTEGER PRIMARY KEY, so all *_increments_primary variants compile to INTEGER PRIMARY KEY AUTOINCREMENT (the rowid alias is 64-bit, so big_increments loses no range). The original mapping produced invalid SQL for the tiny and big variants. The unreachable list-form fallback in columnize() now routes through the same mapping instead of appending its own constraint. - MSSQL: T-SQL expects IDENTITY before column constraints, so the PRIMARY KEY is emitted after the nullability clause ([id] INT IDENTITY NOT NULL PRIMARY KEY) instead of the undocumented INT PRIMARY KEY IDENTITY order. - MySQL: an AUTO_INCREMENT column must be defined as a key (error 1075), so non-primary *_increments columns now carry UNIQUE. - Postgres: non-primary serial columns keep the UNIQUE constraint the previous SERIAL UNIQUE mapping carried, staying consistent with MySQL. - Split the SQLite unsupported-types test so each variant actually asserts (the second and third blocks were dead code inside the first assertRaises), add compile assertions for id() and big_increments().primary() on SQLite, and execute an id() table creation against the real test database. - Move the changelog entry to an Unreleased section: 3.0.0 already shipped without this change.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #4
What this does
increments(),tiny_increments()andbig_increments()no longer implicitly mark the column as the table's primary key, aligning them with every other datatype usable as a primary key. This unblocks tables that pair a non-integer primary key (e.g.uuid) with an auto-numbered column — previously that raisedQueryException: table has more than one primary key:.primary()on an*increments()column compiles the PRIMARY KEY inline through dedicated*_increments_primarytype mappings.id()keeps the previous convenience behavior (auto-incrementing big-integer primary key).migrationstable schema declares.primary()explicitly.Schemanow honors explicitly providedconnection_detailsinstead of always reloading them from the config file (same pattern #948 introduced for the QueryBuilder).Migrations relying on
*increments()implying the primary key must add.primary():Platform-SQL fixes added on top of the original PR
The original mappings compiled to invalid SQL on two platforms:
BIGINT PRIMARY KEY AUTOINCREMENTINTEGER PRIMARY KEY AUTOINCREMENTINTEGER PRIMARY KEY; the tiny/big variants were syntax errors. The rowid alias is 64-bit, sobig_incrementsloses no range.INT PRIMARY KEY IDENTITYINT IDENTITY NOT NULL PRIMARY KEYINT UNSIGNED AUTO_INCREMENTINT UNSIGNED AUTO_INCREMENT UNIQUESERIALSERIAL UNIQUESERIAL UNIQUEmapping carried; consistent with MySQL.SQLite still raises a descriptive
QueryExceptionfor non-primary*increments()(SQLite cannot autoincrement a non-PK column).Tests
assertRaises).id()andbig_increments().primary()on SQLite plus an executedid()table creation against the real test database, so invalid AUTOINCREMENT DDL can't slip through string-only tests again.3.x).