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

createMissingTablesAndColumns() fails with references #952

Closed
jschmid opened this issue Jun 11, 2020 · 2 comments
Closed

createMissingTablesAndColumns() fails with references #952

jschmid opened this issue Jun 11, 2020 · 2 comments

Comments

@jschmid
Copy link

jschmid commented Jun 11, 2020

I have what I thought to be a simple schema, but createMissingTablesAndColumns() throught an exception when executing.

Schema

object Connections : IntIdTable() {
    val fromStation = reference("fromStation", Stations)
    val toStation = reference("toStation", Stations)
    val minDuration = integer("minDuration")
    val medianDuration = integer("medianDuration")
    val lastDownload = long("lastDownload")

    init {
        uniqueIndex(fromStation, toStation)
    }
}

object Stations : IdTable<String>() {
    override val id = varchar("id", 9).uniqueIndex().entityId()
    val name = varchar("name", 100)
    val latitude = double("latitude")
    val longitude = double("longitude")
    val type = varchar("type", 100).nullable()

    override val primaryKey = PrimaryKey(id)
}

My call:

val config = HikariConfig().apply {
    // .... using Postgresql
}

val dataSource = HikariDataSource(config)

Database.connect(dataSource)

transaction {
    addLogger(StdOutSqlLogger)
    SchemaUtils.createMissingTablesAndColumns(Connections, Stations)
}

Log:

SQL: CREATE TABLE IF NOT EXISTS stations (id VARCHAR(9) PRIMARY KEY, "name" VARCHAR(100) NOT NULL, latitude DOUBLE PRECISION NOT NULL, longitude DOUBLE PRECISION NOT NULL, "type" VARCHAR(100) NULL)
SQL: ALTER TABLE stations ADD CONSTRAINT stations_id_unique UNIQUE (id)
SQL: CREATE TABLE IF NOT EXISTS connections (id SERIAL PRIMARY KEY, "fromStation" VARCHAR(9) NOT NULL, "toStation" VARCHAR(9) NOT NULL, "minDuration" INT NOT NULL, "medianDuration" INT NOT NULL, "lastDownload" BIGINT NOT NULL, CONSTRAINT fk_connections_fromstation_id FOREIGN KEY ("fromStation") REFERENCES stations(id) ON DELETE RESTRICT ON UPDATE RESTRICT, CONSTRAINT fk_connections_tostation_id FOREIGN KEY ("toStation") REFERENCES stations(id) ON DELETE RESTRICT ON UPDATE RESTRICT)
SQL: ALTER TABLE connections ADD CONSTRAINT connections_fromstation_tostation_unique UNIQUE ("fromStation", "toStation")
SQL: ALTER TABLE connections ADD CONSTRAINT fk_connections_fromstation_id FOREIGN KEY ("fromStation") REFERENCES stations(id) ON DELETE RESTRICT ON UPDATE RESTRICT
SQL: ALTER TABLE connections ADD CONSTRAINT fk_connections_fromstation_id FOREIGN KEY ("fromStation") REFERENCES stations(id) ON DELETE RESTRICT ON UPDATE RESTRICT
SQL: ALTER TABLE connections ADD CONSTRAINT fk_connections_fromstation_id FOREIGN KEY ("fromStation") REFERENCES stations(id) ON DELETE RESTRICT ON UPDATE RESTRICT
Exception in thread "main" org.jetbrains.exposed.exceptions.ExposedSQLException: org.postgresql.util.PSQLException: ERROR: constraint "fk_connections_fromstation_id" for relation "connections" already exists
SQL: [ALTER TABLE connections ADD CONSTRAINT fk_connections_fromstation_id FOREIGN KEY ("fromStation") REFERENCES stations(id) ON DELETE RESTRICT ON UPDATE RESTRICT]
	at org.jetbrains.exposed.sql.statements.Statement.executeIn$exposed_core(Statement.kt:63)
	at org.jetbrains.exposed.sql.Transaction.exec(Transaction.kt:126)
	at org.jetbrains.exposed.sql.Transaction.exec(Transaction.kt:112)
	at org.jetbrains.exposed.sql.Transaction.exec(Transaction.kt:88)
	at org.jetbrains.exposed.sql.Transaction.exec(Transaction.kt:79)
	at org.jetbrains.exposed.sql.SchemaUtils.execStatements(SchemaUtils.kt:174)
	at org.jetbrains.exposed.sql.SchemaUtils.createMissingTablesAndColumns(SchemaUtils.kt:243)
	at org.jetbrains.exposed.sql.SchemaUtils.createMissingTablesAndColumns$default(SchemaUtils.kt:228)

At first I though it was due to the uniqueIndex not working, but when I disable it it still fails.
It looks like it is trying to add the constraints multiple time.

@jschmid
Copy link
Author

jschmid commented Jun 11, 2020

Maybe related to #934 and #866

@Tapac
Copy link
Contributor

Tapac commented Jun 19, 2020

Should be fixed in 0.26.1

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