-
Notifications
You must be signed in to change notification settings - Fork 695
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
Add composite foreign key #1385
Conversation
- `from` and `target` properties are now ordered sets of columns, not just a single columns - adjusted other properties and their usage sites accordingly - added new constructor, accepting map of columns - added plus operation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you very much for fixing issue requested so many time!
I kept a few comments to fix but I think they are all more like a minor fixes
onDelete: ReferenceOption? = null, | ||
name: String? = null | ||
) { | ||
_foreignKeys.add(ForeignKeyConstraint(from.zip(target.columns).toMap(), onUpdate, onDelete, name)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess that check that size of from
and target.columns
is the same. Also, maybe it's worth to check column types?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added check for collections sizes. But columns type check is rather tricky - naive type equality check (from.columnType == target.columnType
) will be too restrictive because SQL (at least postgreSQL) allows:
- referencing from
INT
toSERIAL
- referencing from
INT NOT NULL
toINT NULL
(and vice versa!) - referencing from
VARCHAR(50)
toVARCHAR(100)
(and vice versa!) - referencing from
INT
/SMALLINT
toBIGINT
(and vice versa!)
And definitely, this list is not exhaustive - it's just my experiments in sqlfiddle
So, the new method IColumnType.isCompatibleWith(other: IColumnType) : Boolean
needs to be added first to make this check possible. But it looks like a big research task, at least I couldn't quickly find any docs about types compatibility (which type could be referenced to which in foreign key constraint) for any of SQL dialects.
@@ -26,6 +26,7 @@ open class SpringCoroutineTest : SpringTransactionTestBase() { | |||
|
|||
@RepeatableTest(times = 5) | |||
@Test @Transactional @Commit | |||
// Is this test flaky? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it is.
exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/SchemaUtils.kt
Outdated
Show resolved
Hide resolved
- all referenced columns in fk are unique - amount of referencing columns is equal to referenced (in case of referencing to primary key)
* Libs update: kotlin 1.6.0 kotlin coroutines 1.6.0-RC kotlinx-datetime-jvm 0.3.1 Spring framework 5.3.13 Spring boot 2.6.1 * Add composite foreign key (JetBrains#1385) * optReference column should allow update { it[column] = nullableValue } JetBrains#1275 / Fix test on SQLServer * Performance: cache enumConstants as it makes copy on access * Use h2 version variable in exposed-money (JetBrains#1402) * Better handling for opened result sets + logging * Add H2 version 2.0.202 dialect (JetBrains#1397) * Detekt 1.19.0 * Add H2 version 2.0.202 dialect / Fixes for test configuration * Add H2 version 2.0.202 dialect / Fixes for test configuration #2 * Add H2 version 2.0.202 dialect / Fixes for bitwise operations * Add composite foreign key (JetBrains#1385) / Fix test for SQLServer Co-authored-by: Andrey.Tarashevskiy <fantocci@gmail.com> Co-authored-by: naftalmm <naftalmm@gmail.com> Co-authored-by: Philip Wedemann <22521688+hfhbd@users.noreply.github.com>
This PR closes #511 (and its duplicate #1234).
From the DSL point of view, there was added a new method for the
Table
class (foreignKey
), supposed to be called ininit
block.
It has two overloads, allowing two ways for defining composite foreign keys: