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

Custom foreign key naming #510

Closed
Edvinas01 opened this issue Mar 7, 2019 · 0 comments · Fixed by #771
Closed

Custom foreign key naming #510

Edvinas01 opened this issue Mar 7, 2019 · 0 comments · Fixed by #771

Comments

@Edvinas01
Copy link
Contributor

Edvinas01 commented Mar 7, 2019

I'd like to specify a custom name for a foreign key:

object Users : IdTable<Long>("user") {

    override val id = long("id")
        .primaryKey()
        .autoIncrement("user_id_seq")
        .entityId()

    val documentId = reference(
        onDelete = ReferenceOption.SET_NULL,
        foreign = Documents,
        name = "document_id" // <- can set column name, but foreign key cannot be named.
    ).nullable()
}

However looking at current implementation, it seems that fk_${table_name}_${table_column}_${foreign_column} pattern is used and cannot be changed (notice how refName is constructed):

fun from(fromCol: Column<*>): ForeignKeyConstraint {
require(fromCol.referee != null && (fromCol.onDelete != null || fromCol.onUpdate != null)) { "$fromCol does not reference anything" }
val targetColumn = fromCol.referee!!
val t = TransactionManager.current()
val refName = t.quoteIfNecessary(t.cutIfNecessary("fk_${fromCol.table.tableName}_${fromCol.name}_${targetColumn.name}")).inProperCase()
return ForeignKeyConstraint(refName,
t.identity(targetColumn.table), t.identity(targetColumn),
t.identity(fromCol.table), t.identity(fromCol),
fromCol.onUpdate ?: ReferenceOption.NO_ACTION,
fromCol.onDelete ?: ReferenceOption.NO_ACTION)
}
}

I guess it kinda relates to #470 which I've opened some time ago.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants