JetBrains / Exposed

Kotlin SQL Framework
http://jetbrains.github.io/Exposed/
Apache License 2.0
8.37k stars 694 forks source link

SQLite table ignoring primaryKey override #1258

Closed lcsphantom closed 3 years ago

lcsphantom commented 3 years ago

Issue: While using SQLite3 dialect and trying to create a table with autoincrement() and overriding primaryKey, the generated Table does not increment or insert the primary key value.

Computer: Linux Mint 20.1 IntelliJ 2021.1.1 (Ultimate Edition) April 30, 2021

How to replicate: Gradle

implementation ("org.xerial:sqlite-jdbc:3.34.0")
implementation("org.jetbrains.exposed:exposed-core:0.31.1")

Connection

Database.connect("jdbc:sqlite:/database/test.db", driver = "org.sqlite.JDBC")

Table

object Users : Table(name = "User") {
    val id = integer("id").autoIncrement()
    val name = varchar("name", length = 50)

    override val primaryKey: PrimaryKey?
        get() = PrimaryKey(id, name = "PK_USER_ID")
}

Generated SQL CREATE TABLE IF NOT EXISTS "User" (id INT, "name" VARCHAR(50) NOT NULL, password VARCHAR(50) NOT NULL, email VARCHAR(100) NOT NULL, username VARCHAR(100) NOT NULL, CONSTRAINT PK_USER_ID PRIMARY KEY (id))

Fix but not ideal (deprecrated method primaryKey()):

Table

object Users : Table(name = "User") {
    val id = integer("id").autoIncrement().primaryKey()
    val name = varchar("name", length = 50)
}

Generated SQL CREATE TABLE IF NOT EXISTS "User" (id INTEGER PRIMARY KEY AUTOINCREMENT, "name" VARCHAR(50) NOT NULL, password VARCHAR(50) NOT NULL, email VARCHAR(100) NOT NULL, username VARCHAR(100) NOT NULL)

Related issue (to some extent / outdated code)

669

lcsphantom commented 3 years ago

I found the issue while looking in Column.kt and I am opening a pull request to fix this issue, let me know if I missed something and/or if I need to improve anything. Also feel free to just reject the pull request.

Tapac commented 3 years ago

Hi @lcsphantom , thank you for a report and PR. The sad thing is that your code doesn't compile and the problem is more complex as to define named primary autoincrement column the CONSTRAINT pk_name PRIMARY KEY AUTOINCREMENT syntax should be used.

I'll push a fix and close your PR.