Skip to content

Support multi-column foreign keys in SQL#1494

Merged
arcuri82 merged 32 commits intomasterfrom
sql-multi-column-foreign-keys
Apr 23, 2026
Merged

Support multi-column foreign keys in SQL#1494
arcuri82 merged 32 commits intomasterfrom
sql-multi-column-foreign-keys

Conversation

@jgaleotti
Copy link
Copy Markdown
Collaborator

support for multi-column foreign keys in SQL.

jgaleotti added 19 commits April 1, 2026 18:06
… into shared base classes (MYSQL, POSTGRES and H2)
… `NULL` values. In SQL, As `NULL` values are considered unknown (`NULL=NULL` evaluates to false),
…bleString` validate both primary key ID and column name, and add tests for multi-column foreign key support.
@jgaleotti jgaleotti requested a review from arcuri82 April 7, 2026 22:11
*/
sourceColumn: String,
/**
* The position in the list of SQL insertion actions
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

position??? if that is indeed the case, it seems very brittle, as SQL actions can be inserted/deleted during mutation

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are right. This description does not correctly explains what uniqueId is.

* names of columns that form a composite foreign key
* together with this one.
*/
val otherSourceColumnsInCompositeFK: List<String> = emptyList()
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does it contain itself? i.e., sourceColumn? clarify

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, shouldn't it rather be a Set?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I clarified and changed the field from List to Set.

.flatMap { it.flatView().asSequence() }
.filterIsInstance<SqlForeignKeyGene>()
.filter { it.uniqueId == uniqueId && it !== this && it.targetTable == targetTable }
.filter { otherSourceColumnsInCompositeFK.contains(it.name) }
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the name the same as sourceColumn??? i mean, here it is a FK. shouldn't be something like otherTargetColumnsInCompositeFK? bit confused here... maybe add some more comments, and rename variables if needed

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will refactor this code to make it more readable


val sourceColumns: Set<Column>,
/**
* The columns in the source table that are referenced by this foreign key
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are referenced by? you mean something like define?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will improve the Javadoc

* The columns in the target table that are referenced by this foreign key.
* The order should match the order of the source columns.
*/
val targetColumns: List<Column>
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't track sourceTableId as well?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ForeignKey is contained within a data class Table. Therefore I think it is redundant to include the sourceTableId here.


val fks = actions[i].seeTopGenes()
val currentAction = actions[i]
val currentFkGenes = currentAction.seeTopGenes()
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can use seeAllGenes()

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok. I will remove the flatview then

Comment thread core/src/main/kotlin/org/evomaster/core/sql/SqlActionUtils.kt Outdated
if (type == DatabaseType.POSTGRES) {
sql += " CASCADE";
}
statement.executeUpdate(sql);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thx. this fix seems not related to PK/FK, isn't it? for future, it would had been better to have in a separate PR

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree. Unfortunately it was blocking my progress, so I pushed as a change here. But next time I will try to have a separate small PR.

@jgaleotti jgaleotti requested a review from arcuri82 April 23, 2026 10:21
@arcuri82 arcuri82 merged commit 1ab4736 into master Apr 23, 2026
14 checks passed
@arcuri82 arcuri82 deleted the sql-multi-column-foreign-keys branch April 23, 2026 11:28
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

Successfully merging this pull request may close these issues.

2 participants