Exposed: Do not use foreign keys

Created on 2 Nov 2017  路  3Comments  路  Source: JetBrains/Exposed

micro-service,multi-databases,multi-tables...DBA said it does not recommend the use of foreign keys.
How to write the code?

  • From the official website of the demo
object Users : Table() {
    val id = varchar("id", 10).primaryKey() // Column<String>
    val name = varchar("name", length = 50) // Column<String>
    val cityId = (integer("city_id") references Cities.id).nullable() // Column<Int?>
}

object Cities : Table() {
    val id = integer("id").autoIncrement().primaryKey() // Column<Int>
    val name = varchar("name", 50) // Column<String>
}

Most helpful comment

Just don't use references and manage everything manually.
val cityId = integer("city_id").entityId().nullable()

All 3 comments

I do not quite understand what do you mean by "not recommend" and what code do you want to write?

For example, User table and bank table is a one-to-one relationship.However, the number of users soared, we need to split the user table. At this point, the foreign key will bring great troubles to the DBA.
So, I want to remove the foreign key.

eg.

object Users : Table() {
    val id = varchar("id", 10).primaryKey() // Column<String>
    val name = varchar("name", length = 50) // Column<String>
    //val cityId = (integer("city_id") references Cities.id).nullable() // before modification
    val cityId = integer("city_id").nullable()
}

object Cities : Table() {
    val id = integer("id").autoIncrement().primaryKey() // Column<Int>
    val name = varchar("name", 50) // Column<String>
}

Just don't use references and manage everything manually.
val cityId = integer("city_id").entityId().nullable()

Was this page helpful?
0 / 5 - 0 ratings

Related issues

power721 picture power721  路  3Comments

gertvdijk picture gertvdijk  路  4Comments

gcscaglia picture gcscaglia  路  3Comments

barry-m picture barry-m  路  3Comments

x3ro picture x3ro  路  5Comments