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

Naming Conflict with current Foreign Key auto-naming #804

Closed
ratiofu opened this issue Feb 21, 2020 · 3 comments
Closed

Naming Conflict with current Foreign Key auto-naming #804

ratiofu opened this issue Feb 21, 2020 · 3 comments

Comments

@ratiofu
Copy link

ratiofu commented Feb 21, 2020

This is a follow-up to #510 and #701. The current strategy for auto-naming foreign key constraints can fail for a particular combination of table names and column names (and does for us). Given these table definitions:

object Root : LongIdTable("root")
object SomeType : LongIdTable("some_type") {
  init { id.references(Root.id) }
}
object Some : LongIdTable("some") {
  val type = reference("type_id", SomeType.id)
}

this test will fail:

@Test
fun `generates different FK constraint names for H2`() {
  val database = Database.connect("jdbc:h2:mem:TEST_FK_DDL", "org.h2.Driver", "root", "")
  transaction(database) {
    val ddl = listOf(Root, SomeType, Some).flatMap { it.ddl }.joinToString(";\n\n")
    exec(ddl) // <-- throws `Constraint "FK_SOME_TYPE_ID_ID" already exists`
  }
}

with:

org.h2.jdbc.JdbcSQLSyntaxErrorException: Constraint "FK_SOME_TYPE_ID_ID" already exists;

The root cause being that both table some_type and some define a foreign key constraint auto-named as "FK_SOME_TYPE_ID_ID"

At a minimum, the target table name should be part of the automatically generated constraint name, although even then a naming conflict may be possible. The preferred solution would be to support custom foreign key names, as per #510. That issue is labeled as "good-first-issue" and I looked into how that could be accomplished, but I could not get the local Gradle build to work on my Mac; it hangs with

Exception in thread "driver_launched_mysqld_1" com.mysql.management.util.WrappedException: java.io.IOException: Cannot run program "[...]test-c.mxj/mysql-c.mxj/bin/mysqld": error=86, Bad CPU type in executable

Do you have a "Contributor Guide" somewhere? This is empty.

@Tapac
Copy link
Contributor

Tapac commented Feb 26, 2020

Thank you for a report, in the next release, there will be the possibility to define foreign keys names.

I'll actualize the wiki page soon, but could you tell me which MacOS version do you use as I run tests via Gradle on El Capitan 10.11.6 without any problems.

@ratiofu
Copy link
Author

ratiofu commented Mar 2, 2020

I am on Catalina, 10.15.3

@ratiofu
Copy link
Author

ratiofu commented Mar 4, 2022

Considering that #510 has long been resolved, I will close this.

@ratiofu ratiofu closed this as completed Mar 4, 2022
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