Exposed: Duplicate columns generated

Created on 15 Nov 2018  路  3Comments  路  Source: JetBrains/Exposed

version: 0.11.2
Issue: Exposed generated duplicate columns
Transaction attempt #0 failed: org.h2.jdbc.JdbcSQLException: Duplicate column name "NAME";

Code:

object Users : IntIdTable() {
    val name = varchar("name", length = 50).uniqueIndex()
}

class User(id: EntityID<Int>) : IntEntity(id) {
    companion object : IntEntityClass<User>(Users)

    var name by Users.name
}

object Groups : IntIdTable() {
    val name = Users.varchar("name", length = 50)
}

class Group(id: EntityID<Int>) : IntEntity(id) {
    companion object : IntEntityClass<Group>(Groups)

    var name by Groups.name
}

fun main(args: Array<String>) {
    Database.connect("jdbc:h2:mem:test", driver = "org.h2.Driver")

    transaction {
        addLogger(StdOutSqlLogger)

        SchemaUtils.create(Users, Groups)
    }
}

Exception:

11:05:08.950 [main] DEBUG Exposed - CREATE TABLE IF NOT EXISTS USERS (ID INT AUTO_INCREMENT PRIMARY KEY, NAME VARCHAR(50) NOT NULL, NAME VARCHAR(50) NOT NULL)
SQL: CREATE TABLE IF NOT EXISTS USERS (ID INT AUTO_INCREMENT PRIMARY KEY, NAME VARCHAR(50) NOT NULL, NAME VARCHAR(50) NOT NULL)
11:05:08.954 [main] INFO Exposed - Transaction attempt #0 failed: org.h2.jdbc.JdbcSQLException: Duplicate column name "NAME"; SQL statement:
CREATE TABLE IF NOT EXISTS USERS (ID INT AUTO_INCREMENT PRIMARY KEY, NAME VARCHAR(50) NOT NULL, NAME VARCHAR(50) NOT NULL) [42121-197]. Statement(s): CREATE TABLE IF NOT EXISTS USERS (ID INT AUTO_INCREMENT PRIMARY KEY, NAME VARCHAR(50) NOT NULL, NAME VARCHAR(50) NOT NULL)
org.jetbrains.exposed.exceptions.ExposedSQLException: org.h2.jdbc.JdbcSQLException: Duplicate column name "NAME"; SQL statement:
CREATE TABLE IF NOT EXISTS USERS (ID INT AUTO_INCREMENT PRIMARY KEY, NAME VARCHAR(50) NOT NULL, NAME VARCHAR(50) NOT NULL) [42121-197]
    at org.jetbrains.exposed.sql.statements.Statement.executeIn$exposed(Statement.kt:61)
    at org.jetbrains.exposed.sql.Transaction.exec(Transaction.kt:128)
    at org.jetbrains.exposed.sql.Transaction.exec(Transaction.kt:122)
    at org.jetbrains.exposed.sql.Transaction.exec(Transaction.kt:101)
    at org.jetbrains.exposed.sql.Transaction.exec(Transaction.kt:92)
    at org.jetbrains.exposed.sql.SchemaUtils.create(SchemaUtils.kt:160)
    at com.har01d.demo.springbootkotlin.SpringBootKotlinApplicationKt$main$1.invoke(SpringBootKotlinApplication.kt:27)
    at com.har01d.demo.springbootkotlin.SpringBootKotlinApplicationKt$main$1.invoke(SpringBootKotlinApplication.kt)
    at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.inTopLevelTransaction(ThreadLocalTransactionManager.kt:104)
    at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.transaction(ThreadLocalTransactionManager.kt:75)
    at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.transaction(ThreadLocalTransactionManager.kt:58)
    at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.transaction$default(ThreadLocalTransactionManager.kt:58)
    at com.har01d.demo.springbootkotlin.SpringBootKotlinApplicationKt.main(SpringBootKotlinApplication.kt:24)
Caused by: org.h2.jdbc.JdbcSQLException: Duplicate column name "NAME"; SQL statement:
CREATE TABLE IF NOT EXISTS USERS (ID INT AUTO_INCREMENT PRIMARY KEY, NAME VARCHAR(50) NOT NULL, NAME VARCHAR(50) NOT NULL) [42121-197]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:357)
    at org.h2.message.DbException.get(DbException.java:179)
    at org.h2.message.DbException.get(DbException.java:155)
    at org.h2.table.Table.setColumns(Table.java:431)
    at org.h2.table.TableBase.<init>(TableBase.java:46)
    at org.h2.mvstore.db.MVTable.<init>(MVTable.java:128)
    at org.h2.mvstore.db.MVTableEngine.createTable(MVTableEngine.java:109)
    at org.h2.mvstore.db.MVTableEngine.createTable(MVTableEngine.java:42)
    at org.h2.schema.Schema.createTable(Schema.java:670)
    at org.h2.command.ddl.CreateTable.update(CreateTable.java:100)
    at org.h2.command.CommandContainer.update(CommandContainer.java:102)
    at org.h2.command.Command.executeUpdate(Command.java:261)
    at org.h2.jdbc.JdbcPreparedStatement.executeUpdateInternal(JdbcPreparedStatement.java:199)
    at org.h2.jdbc.JdbcPreparedStatement.executeUpdate(JdbcPreparedStatement.java:153)
    at org.jetbrains.exposed.sql.Transaction$exec$2.executeInternal(Transaction.kt:105)
    at org.jetbrains.exposed.sql.statements.Statement.executeIn$exposed(Statement.kt:59)
    ... 12 common frames omitted
bug

Most helpful comment

Sorry, I used Intellij IDEA's copy function.

All 3 comments

Why you use val name = Users.varchar("name", length = 50) in Groups table definition?
This will add "name" column to Users instead of Groups.

Sorry, I used Intellij IDEA's copy function.

Yeah, this copy sometimes requires more time than it saves :D

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kszymanski85 picture kszymanski85  路  4Comments

supertote picture supertote  路  3Comments

raderio picture raderio  路  4Comments

blackmo18 picture blackmo18  路  3Comments

michele-grifa picture michele-grifa  路  4Comments