-
Notifications
You must be signed in to change notification settings - Fork 350
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 support for forein keys in schema generation within aggregates #1629
Conversation
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.
This looks pretty good already.
I see just one major problem: It won't work for nested lists or maps.
If you could add a test for those and fix the resulting issues it would be ready to go.
...ng-data-jdbc/src/main/java/org/springframework/data/jdbc/core/mapping/schema/ForeignKey.java
Outdated
Show resolved
Hide resolved
...ng-data-jdbc/src/main/java/org/springframework/data/jdbc/core/mapping/schema/ForeignKey.java
Show resolved
Hide resolved
...ng-data-jdbc/src/main/java/org/springframework/data/jdbc/core/mapping/schema/SchemaDiff.java
Outdated
Show resolved
Hide resolved
Original pull request spring-projects#1629
Hey! I did some polishing and added the test for nested collections. Please give me more information if I got your comment wrong. |
I'm sorry, I wasn't very precise when I said nested collections. What I really should have said is nested lists or maps where the entities don't have ids. I modified the domain model of your test to clarify what I mean: https://github.com/spring-projects/spring-data-relational/tree/pr/schema-generation-fks |
@schauder Oh, sorry don't quite understnad that case. I thought that every entity (class marked by Table annotation) must have field with Id annotation. If not I can't see where foreign key of OtherTable should reference in Tables. I can suggest that it can reference to composite key using all fields of Tables (then it makes sense to use collection of column names). But in that case why do you talk only about Map and List but not about Set? Sorry if my questions are stupid, just something doesn't match for me =) |
Not at all stupid questions. Inner entities of 1:1 relations or in lists or maps don't need ids. I actually recommend against ids. You can see examples of this in Line 879 in 7f649c4
And the matching schema in Line 149 in 7f649c4
|
Original pull request spring-projects#1629
@schauder Oh, okay, now I see. Please check my last commit, I fixed it. Just one questions to ensure. Is my guess right, that if parent entity already have composite primary key than "idColumn" of MappedCollection doesn't work and child entity reuses names from parent without changing (only adding keyColumn to primary key)? Please look at top level entity (ListOfMapOfNoIdTables) in tests. I used "idColumn" there. Can you please check that primary key column names are correct in MapOfNoIdTables and NoIdTable based on that annotation. |
Original pull request spring-projects#1629
@schauder Hey! In last commit I added processing one-to-one relations, also simplified code a little, fixed bugs and added few tricky test cases.
What is primary key in NoIdTable? one_to_one_level1 or additional_one_to_one_level2 or both or none? Because primary key is not nullable but what if one of the referencing fields will be null? Now I just pick first one available and make it to be primary key. Maybe we don't need to create primary key at all in such cases? Here is even more interesting example:
What schema should be generated here? |
If there are multiple child references of the same type and that type doesn't have an id, we strictly speaking shouldn't create PK. But I would be willing to fix this with a later change. |
Circular references aren't allowed and should fail, preferable with an expressive exception. |
Yeah, that looks correct. |
I merged this after removing the tests for circular references, since these are not supported anyway. Thanks. |
Done:
Always drop foreign keys before table drop. Always add foreign keys after table creation. The same for fields. (to be sore that referenced column always exists).
Closes #1599
Related tickets #756, #1600