I'm developing a system in which a ClassA
object can have multiple ClassB
and ClassC
objects.
enter image description here
And for store these objects in a relational DB model I create the following tables: enter image description here
This is a simplified example of the real system.
My problem arises when I need to update ClassA
objects because I need to check if the modified object has more ClassA
and ClassB
objects or less and then update the ClassA
table and add, update or delete corresponding rows from ClassBObjectsPerClassAObject
and ClassCObjectsPerClassAObject
tables. So my approach was to keep the original object unmodified in the persistence layer until the modified object comes back from the interface and then compare them, but with this approach, all objects need to be duplicated. Is there a better approach, method, or design pattern that I can use for this kind of problem?
I'm using Python with SQLAlchemy and MySql in the persistence layer, flask in the services layer, and Angular in the frontend
-
Your class diagram is ambiguous, ince multiplicity is expected at both ends. Is it many to many (0..* - 0..*) or one to many (1 - 0..*)?Christophe– Christophe2021年11月27日 20:36:54 +00:00Commented Nov 27, 2021 at 20:36
-
It's one to many (1 - 0..*)Caeta– Caeta2021年11月27日 20:44:33 +00:00Commented Nov 27, 2021 at 20:44
-
2is there a reason why you have the intermediaty table and not add the foreign key in the class b table?Christophe– Christophe2021年11月27日 21:27:54 +00:00Commented Nov 27, 2021 at 21:27
-
mm no, just, I hadn't thought of it that way. I should try it to check if it works in the real systemCaeta– Caeta2021年11月27日 21:46:00 +00:00Commented Nov 27, 2021 at 21:46
1 Answer 1
Considering that these are one-to-many associations (as confirmed in the comments), it appears that your relational schema could be simplified, getting rid of intermediary association tables.
Using a simpler foreign key mapping would mean that the tables for classes B and C could each get the foreign key referring to class A's id. THe RDBMS allows bidirectional navigation. Keep the association tables for many-to-many associations.
Then for the modification, it should not be an issue either, thanks to the isolation levels in the database: if you update an A, just do not commit immediately: do the related updates that are needed and commit once you have a consistent state (or rollback if yu cannot complete your transaction; your app would then have to reread the initial state of the A from the DB). The Unit of Work pattern could help you to deal with these kind of consistency issues.
Explore related questions
See similar questions with these tags.