question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Postgresql uniqueIndex error with sheme.

See original GitHub issue

This is happening only with scheme.

Code:

fun main()
{
	Database.connect(url = "jdbc:postgresql://localhost:5432/test_database",
					 driver = "org.postgresql.Driver",
					 user = "test_user",
					 password = "test_password")

	transaction {
		createSchema("user")
		SchemaUtils.createMissingTablesAndColumns(UsersTable)
	}
}

fun Transaction.createSchema(name: String) { exec("CREATE SCHEMA IF NOT EXISTS \"$name\"") }

object UsersTable : IntIdTable("user.users")
{
	val name = varchar("name", 200)
	val email = varchar("email", 100).uniqueIndex("users_email_unique")
	val password = varchar("password", 100)
}

Error:

15:53:49.891 [main] DEBUG Exposed - CREATE SCHEMA IF NOT EXISTS "users"
15:53:54.781 [main] INFO Exposed - Preparing create tables statements took 172ms
15:53:55.106 [main] DEBUG Exposed - CREATE TABLE IF NOT EXISTS users.users (id SERIAL PRIMARY KEY, "name" VARCHAR(200) NOT NULL, email VARCHAR(100) NOT NULL, "password" VARCHAR(100) NOT NULL)
15:53:55.231 [main] DEBUG Exposed - ALTER TABLE users.users ADD CONSTRAINT users_email_unique UNIQUE (email)
15:53:55.267 [main] INFO Exposed - Executing create tables statements took 486ms
15:53:55.674 [main] INFO Exposed - Extracting table columns took 407ms
15:53:55.722 [main] INFO Exposed - Extracting column constraints took 42ms
15:53:55.722 [main] INFO Exposed - Preparing alter table statements took 455ms
15:53:55.747 [main] DEBUG Exposed - ALTER TABLE users.users ADD id SERIAL PRIMARY KEY
15:53:55.761 [main] WARN Exposed - Transaction attempt #0 failed: org.postgresql.util.PSQLException: ERROR: column "id" of relation "users" already exists. Statement(s): ALTER TABLE users.users ADD id SERIAL PRIMARY KEY
org.jetbrains.exposed.exceptions.ExposedSQLException: org.postgresql.util.PSQLException: ERROR: column "id" of relation "users" already exists
	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:170)
	at org.jetbrains.exposed.sql.SchemaUtils.createMissingTablesAndColumns(SchemaUtils.kt:239)
	at org.jetbrains.exposed.sql.SchemaUtils.createMissingTablesAndColumns$default(SchemaUtils.kt:224)
	at MainKt$main$1.invoke(Main.kt:14)
	at MainKt$main$1.invoke(Main.kt)
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt$inTopLevelTransaction$1.invoke(ThreadLocalTransactionManager.kt:156)
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt$inTopLevelTransaction$2.invoke(ThreadLocalTransactionManager.kt:197)
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.keepAndRestoreTransactionRefAfterRun(ThreadLocalTransactionManager.kt:205)
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.inTopLevelTransaction(ThreadLocalTransactionManager.kt:196)
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt$transaction$1.invoke(ThreadLocalTransactionManager.kt:134)
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.keepAndRestoreTransactionRefAfterRun(ThreadLocalTransactionManager.kt:205)
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.transaction(ThreadLocalTransactionManager.kt:106)
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.transaction(ThreadLocalTransactionManager.kt:104)
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.transaction$default(ThreadLocalTransactionManager.kt:103)
	at MainKt.main(Main.kt:12)
	at MainKt.main(Main.kt)
Caused by: org.postgresql.util.PSQLException: ERROR: column "id" of relation "users" already exists
	at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2433)
	at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2178)
	at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:306)
	at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:441)
	at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:365)
	at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:155)
	at org.postgresql.jdbc.PgPreparedStatement.executeUpdate(PgPreparedStatement.java:132)
	at org.jetbrains.exposed.sql.statements.jdbc.JdbcPreparedStatementImpl.executeUpdate(JdbcPreparedStatementImpl.kt:23)
	at org.jetbrains.exposed.sql.Transaction$exec$2.executeInternal(Transaction.kt:93)
	at org.jetbrains.exposed.sql.statements.Statement.executeIn$exposed_core(Statement.kt:61)
	... 20 common frames omitted
15:53:56.051 [main] DEBUG Exposed - CREATE SCHEMA IF NOT EXISTS "users"
15:53:56.079 [main] INFO Exposed - Preparing create tables statements took 27ms
15:53:56.081 [main] DEBUG Exposed - CREATE TABLE IF NOT EXISTS users.users (id SERIAL PRIMARY KEY, "name" VARCHAR(200) NOT NULL, email VARCHAR(100) NOT NULL, "password" VARCHAR(100) NOT NULL)
15:53:56.084 [main] DEBUG Exposed - ALTER TABLE users.users ADD CONSTRAINT users_email_unique UNIQUE (email)
15:53:56.084 [main] WARN Exposed - Transaction attempt #1 failed: org.postgresql.util.PSQLException: ERROR: relation "users_email_unique" already exists. Statement(s): ALTER TABLE users.users ADD CONSTRAINT users_email_unique UNIQUE (email)
org.jetbrains.exposed.exceptions.ExposedSQLException: org.postgresql.util.PSQLException: ERROR: relation "users_email_unique" already exists
	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:170)
	at org.jetbrains.exposed.sql.SchemaUtils.createMissingTablesAndColumns(SchemaUtils.kt:231)
	at org.jetbrains.exposed.sql.SchemaUtils.createMissingTablesAndColumns$default(SchemaUtils.kt:224)
	at MainKt$main$1.invoke(Main.kt:14)
	at MainKt$main$1.invoke(Main.kt)
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt$inTopLevelTransaction$1.invoke(ThreadLocalTransactionManager.kt:156)
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt$inTopLevelTransaction$2.invoke(ThreadLocalTransactionManager.kt:197)
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.keepAndRestoreTransactionRefAfterRun(ThreadLocalTransactionManager.kt:205)
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.inTopLevelTransaction(ThreadLocalTransactionManager.kt:196)
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt$transaction$1.invoke(ThreadLocalTransactionManager.kt:134)
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.keepAndRestoreTransactionRefAfterRun(ThreadLocalTransactionManager.kt:205)
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.transaction(ThreadLocalTransactionManager.kt:106)
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.transaction(ThreadLocalTransactionManager.kt:104)
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.transaction$default(ThreadLocalTransactionManager.kt:103)
	at MainKt.main(Main.kt:12)
	at MainKt.main(Main.kt)
Caused by: org.postgresql.util.PSQLException: ERROR: relation "users_email_unique" already exists
	at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2433)
	at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2178)
	at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:306)
	at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:441)
	at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:365)
	at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:155)
	at org.postgresql.jdbc.PgPreparedStatement.executeUpdate(PgPreparedStatement.java:132)
	at org.jetbrains.exposed.sql.statements.jdbc.JdbcPreparedStatementImpl.executeUpdate(JdbcPreparedStatementImpl.kt:23)
	at org.jetbrains.exposed.sql.Transaction$exec$2.executeInternal(Transaction.kt:93)
	at org.jetbrains.exposed.sql.statements.Statement.executeIn$exposed_core(Statement.kt:61)
	... 20 common frames omitted
15:53:56.130 [main] DEBUG Exposed - CREATE SCHEMA IF NOT EXISTS "users"
15:53:56.183 [main] INFO Exposed - Preparing create tables statements took 25ms
15:53:56.187 [main] DEBUG Exposed - CREATE TABLE IF NOT EXISTS users.users (id SERIAL PRIMARY KEY, "name" VARCHAR(200) NOT NULL, email VARCHAR(100) NOT NULL, "password" VARCHAR(100) NOT NULL)
15:53:56.189 [main] DEBUG Exposed - ALTER TABLE users.users ADD CONSTRAINT users_email_unique UNIQUE (email)
15:53:56.190 [main] WARN Exposed - Transaction attempt #2 failed: org.postgresql.util.PSQLException: ERROR: relation "users_email_unique" already exists. Statement(s): ALTER TABLE users.users ADD CONSTRAINT users_email_unique UNIQUE (email)
org.jetbrains.exposed.exceptions.ExposedSQLException: org.postgresql.util.PSQLException: ERROR: relation "users_email_unique" already exists
	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:170)
	at org.jetbrains.exposed.sql.SchemaUtils.createMissingTablesAndColumns(SchemaUtils.kt:231)
	at org.jetbrains.exposed.sql.SchemaUtils.createMissingTablesAndColumns$default(SchemaUtils.kt:224)
	at MainKt$main$1.invoke(Main.kt:14)
	at MainKt$main$1.invoke(Main.kt)
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt$inTopLevelTransaction$1.invoke(ThreadLocalTransactionManager.kt:156)
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt$inTopLevelTransaction$2.invoke(ThreadLocalTransactionManager.kt:197)
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.keepAndRestoreTransactionRefAfterRun(ThreadLocalTransactionManager.kt:205)
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.inTopLevelTransaction(ThreadLocalTransactionManager.kt:196)
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt$transaction$1.invoke(ThreadLocalTransactionManager.kt:134)
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.keepAndRestoreTransactionRefAfterRun(ThreadLocalTransactionManager.kt:205)
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.transaction(ThreadLocalTransactionManager.kt:106)
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.transaction(ThreadLocalTransactionManager.kt:104)
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.transaction$default(ThreadLocalTransactionManager.kt:103)
	at MainKt.main(Main.kt:12)
	at MainKt.main(Main.kt)
Caused by: org.postgresql.util.PSQLException: ERROR: relation "users_email_unique" already exists
	at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2433)
	at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2178)
	at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:306)
	at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:441)
	at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:365)
	at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:155)
	at org.postgresql.jdbc.PgPreparedStatement.executeUpdate(PgPreparedStatement.java:132)
	at org.jetbrains.exposed.sql.statements.jdbc.JdbcPreparedStatementImpl.executeUpdate(JdbcPreparedStatementImpl.kt:23)
	at org.jetbrains.exposed.sql.Transaction$exec$2.executeInternal(Transaction.kt:93)
	at org.jetbrains.exposed.sql.statements.Statement.executeIn$exposed_core(Statement.kt:61)
	... 20 common frames omitted
Exception in thread "main" org.jetbrains.exposed.exceptions.ExposedSQLException: org.postgresql.util.PSQLException: ERROR: relation "users_email_unique" already exists
SQL: [ALTER TABLE users.users ADD CONSTRAINT users_email_unique UNIQUE (email)]
	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:170)
	at org.jetbrains.exposed.sql.SchemaUtils.createMissingTablesAndColumns(SchemaUtils.kt:231)
	at org.jetbrains.exposed.sql.SchemaUtils.createMissingTablesAndColumns$default(SchemaUtils.kt:224)
	at MainKt$main$1.invoke(Main.kt:14)
	at MainKt$main$1.invoke(Main.kt)
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt$inTopLevelTransaction$1.invoke(ThreadLocalTransactionManager.kt:156)
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt$inTopLevelTransaction$2.invoke(ThreadLocalTransactionManager.kt:197)
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.keepAndRestoreTransactionRefAfterRun(ThreadLocalTransactionManager.kt:205)
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.inTopLevelTransaction(ThreadLocalTransactionManager.kt:196)
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt$transaction$1.invoke(ThreadLocalTransactionManager.kt:134)
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.keepAndRestoreTransactionRefAfterRun(ThreadLocalTransactionManager.kt:205)
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.transaction(ThreadLocalTransactionManager.kt:106)
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.transaction(ThreadLocalTransactionManager.kt:104)
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.transaction$default(ThreadLocalTransactionManager.kt:103)
	at MainKt.main(Main.kt:12)
	at MainKt.main(Main.kt)
Caused by: org.postgresql.util.PSQLException: ERROR: relation "users_email_unique" already exists
	at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2433)
	at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2178)
	at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:306)
	at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:441)
	at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:365)
	at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:155)
	at org.postgresql.jdbc.PgPreparedStatement.executeUpdate(PgPreparedStatement.java:132)
	at org.jetbrains.exposed.sql.statements.jdbc.JdbcPreparedStatementImpl.executeUpdate(JdbcPreparedStatementImpl.kt:23)
	at org.jetbrains.exposed.sql.Transaction$exec$2.executeInternal(Transaction.kt:93)
	at org.jetbrains.exposed.sql.statements.Statement.executeIn$exposed_core(Statement.kt:61)
	... 20 more

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
AntonAbcommented, Feb 1, 2021

0.29.1 has the same error too. It is because Queries.kt line 307

val existingTableIndices = existingIndices[table].orEmpty().filterFKeys().filterInternalIndices()

existingTableIndices contains Primary key as EntityIDColumnType instead of Primary index type. That’s why in this line

missingIndices.addAll(mappedIndices.subtract(existingTableIndices))

Primary key index is marked as missing. My simple work around:

fun List<Index>.fixEntityIdColumns(): List<Index> {
        return this.map {
            val columns = it.columns.map { col ->
                when (col.columnType.javaClass) {
                    EntityIDColumnType::class.java -> {
                        (col.columnType as EntityIDColumnType<*>).idColumn
                    }
                    else -> col
                }
            }
            it.copy(columns = columns)
        }
    }

and

val existingTableIndices = existingIndices[table].orEmpty().filterFKeys().filterInternalIndices().fixEntityIdColumns()
1reaction
Tapaccommented, Aug 1, 2020

Should be fixed in the next release. Please report if issue will still exist.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unexpected unique constraint violation during update
Is there anything I could try to resolve the problem (different index type, only constraint, only index)? I noticed that this error very...
Read more >
Documentation: 15: 11.6. Unique Indexes - PostgreSQL
When an index is declared unique, multiple table rows with equal indexed values are not allowed. Null values are not considered equal. A...
Read more >
could not create unique index DETAIL: Key (id)=(3105115) is ...
I tried to alter one table. ALTER TABLE mytable ALTER COLUMN some_column type NUMERIC;. But I keep getting error.
Read more >
PostgreSQL - UNIQUE Index - GeeksforGeeks
In PostgreSQL, the UNIQUE index to ensure the uniqueness of values in one or more columns. ... This should raise an error. Output:....
Read more >
How to handle duplicates and unique index conflicts when ...
Before EDB Postgres Advanced Server version -13, EDB*Loader would abort the load if it hit the duplicate records violating unique indexes ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found