Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SQLite] fixing a bug in creating table with autoInc column and custom primarykey #755

Merged
merged 1 commit into from
Jan 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class Column<T>(val table: Table, val name: String, override val columnType: ICo

when {
!isPKColumn && isSQLiteAutoIncColumn -> tr.throwUnsupportedException("Auto-increment could be applied only to primary key column")
isSQLiteAutoIncColumn && !isOneColumnPK() && table.primaryKey != null -> append(currentDialect.dataTypeProvider.integerType())
isSQLiteAutoIncColumn && (!isOneColumnPK() || table.isCustomPKNameDefined()) && table.primaryKey != null -> append(currentDialect.dataTypeProvider.integerType())
else -> append(colType.sqlType())
Comment on lines -70 to 71
Copy link
Contributor Author

Choose a reason for hiding this comment

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

When using SQLite : If isOneColumnPK() = true and the column type is integerAutoincType, then colType.sqlType() returns : "INTEGER PRIMARY KEY AUTOINCREMENT" at the same time when defining a costom primaryKey name, a primary key constraint is generated too. That's why we get the error.
Here I added a condition to prevent defining primary key multiple times.

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,24 @@ class CreateTableTests : DatabaseTestsBase() {
}
}

@Test
fun addOneColumnPrimaryKeyToTableNotH2Test() {
withTables(excludeSettings = listOf(TestDB.H2, TestDB.H2_MYSQL), tables = *arrayOf(Book)) {
val tableProperName = Book.tableName.inProperCase()
val pkConstraintName = Book.primaryKey.name
val id1ProperName = Book.id.name.inProperCase()
val ddlId1 = Book.id.ddl

assertEquals("ALTER TABLE $tableProperName ADD ${Book.id.descriptionDdl()}, ADD CONSTRAINT $pkConstraintName PRIMARY KEY ($id1ProperName)", ddlId1.first())
}
}

object Book : Table("Book") {
val id = integer("id").autoIncrement()

override val primaryKey = PrimaryKey(id, name = "PKConstraintName")
}

object Person : Table("Person") {
val id1 = integer("id1")
val id2 = integer("id2")
Expand Down