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 table ignoring primaryKey override #1258

Closed
lcsphantom opened this issue Jun 6, 2021 · 2 comments
Closed

SQLite table ignoring primaryKey override #1258

lcsphantom opened this issue Jun 6, 2021 · 2 comments

Comments

@lcsphantom
Copy link

lcsphantom commented Jun 6, 2021

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
Copy link
Author

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
Copy link
Contributor

Tapac commented Jul 18, 2021

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.

@Tapac Tapac closed this as completed Jul 18, 2021
SchweinchenFuntik pushed a commit to SchweinchenFuntik/Exposed that referenced this issue Oct 23, 2021
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

No branches or pull requests

2 participants