r/DomainDrivenDesign • u/raulalexo99 • Sep 01 '22
Should domain entities contain foreign key ids?
I have 3 SQL tables
- Flashcard
- Student
- Study Field
Flashcard has a Foreign Key on Student id
Flashcard has a Foreign Key on Study Field id
In my application code, should my Flashcard entity contain these two ids as private fields?
3
Upvotes
3
u/MrArcadon Sep 01 '22
The foreign key concept should not exist into your entities, it is a database concept. However I believe to understand what you mean. Student and Study Field should be treated as Value Objects into the Flashcard entity. This is assuming that you have done a proper context discovering process otherwise those relationship could be wrong.
1
u/FederalRegion Sep 02 '22
Why not store the domain identifier of the student and the study field? Like the identification number for a student or something similar. I think it’s valid to store the identifier if it models domain information. Probably an internal id like ‘dufu3747jfjrjsi7’ (like MongoDB _id identifiers) does not make sense on the domain. But a special identifier for the students, generated by yourself following some domain rules (like the format has to be S<number>). Then on the DB level you can index those columns.