Skip to content
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 capability to modify the linking table even if it is not exposed to satisfy foreign key constraints #285

Closed
Tracked by #1576
Aniruddh25 opened this issue Mar 4, 2022 · 7 comments
Assignees
Labels
enhancement New feature or request move-to-close Lacks clarity or roadmap mismatch mssql pgsql mysql an issue that applies to all relational databases, same as labeling with `mssql` `mysql` and `pgsql`

Comments

@Aniruddh25
Copy link
Contributor

Aniruddh25 commented Mar 4, 2022

In M:N relationships when the linking table is not exposed, we need to make sure mutation on the parent tables trigger deletion of rows in the linking table as well.
E.g. book_author_link should have its row deleted when a book with book_id referenced in that row is deleted from the books table.

@Aniruddh25 Aniruddh25 added this to the M1 milestone Mar 4, 2022
@Aniruddh25 Aniruddh25 added mssql pgsql mysql an issue that applies to all relational databases, same as labeling with `mssql` `mysql` and `pgsql` enhancement New feature or request labels Apr 16, 2022
@Aniruddh25 Aniruddh25 modified the milestones: M1, M1.5 Apr 22, 2022
@Aniruddh25 Aniruddh25 self-assigned this Jul 5, 2022
@Aniruddh25 Aniruddh25 modified the milestones: Oct2022, Sept2022 Aug 24, 2022
@Aniruddh25 Aniruddh25 modified the milestones: Oct2022, Nov2022 Sep 27, 2022
@Aniruddh25 Aniruddh25 modified the milestones: Oct2022, Nov2022 Oct 13, 2022
@severussundar
Copy link
Contributor

severussundar commented Nov 9, 2022

Consider three tables: books, authors and book_author_link tables. There is a M:N relationship between books and authors. book_author_link is the linking table. book_author_link consists of two columns : book_id, author_id as foreign key references to books and authors respectively.

When a delete mutation is executed on either books or authors, the behavior depends on whether CASCADE is set up or not.

  1. When ON DELETE CASCADE is setup for author_id column in the book_author_link, then executing a delete mutation (deleteAuthor) results in deletion of entries in both authors and book_author_link tables.
  2. When ON DELETE CASCADE is not setup, then executing a delete mutation results in an error.

Error Message:
Error msg

IMHO, the current behavior that we observe at the moment is what we would want, as it honors the way the tables (and relationships) are setup.

@Aniruddh25 @yorek
Please kindly share your thoughts.

@Aniruddh25
Copy link
Contributor Author

CASCADE deletes is a good find, we need to document this.
How about updates/inserts? Would the linking table be modified? Would this issue then be same as #479

@severussundar
Copy link
Contributor

severussundar commented Nov 14, 2022

  • When the primary key of a table is not auto-generated, then the update mutation's input type exposes the id field. If the primary key is auto-generated, then the id field is not exposed and updating the id is not possible.
  • For the cases where the id is exposed through the update mutation, when an update to the id is performed, the behavior depends on whether ON UPDATE CASCADE is setup. The behavior is very similar to that of delete. If the update cascade is setup, the mutation succeeds and the update flows to linking table as well.
  • If ON UPDATE CASCADE is not setup, then the mutation results in an error
  • Inserting a new book, author and a relationship between these two, will come under nested mutations.

@Aniruddh25 Aniruddh25 removed this from the Nov2022 milestone Nov 18, 2022
@Aniruddh25 Aniruddh25 added this to the Jan2023 milestone Nov 18, 2022
@severussundar severussundar modified the milestones: Jan2023, Mar2023 Jan 6, 2023
@severussundar severussundar modified the milestones: Mar2023, Apr2023 Mar 7, 2023
@severussundar severussundar modified the milestones: Apr2023, May2023 Apr 11, 2023
@severussundar severussundar modified the milestones: 0.8, June2023 May 10, 2023
@severussundar severussundar modified the milestones: 0.9, 0.10 Jul 10, 2023
@severussundar severussundar modified the milestones: 0.10rc, 0.11rc Oct 11, 2023
@severussundar severussundar removed this from the 0.11rc milestone Nov 16, 2023
@seantleonard
Copy link
Contributor

@severussundar @ayush3797 is this needed for Nested mutations?

@ayush3797
Copy link
Contributor

@severussundar @ayush3797 is this needed for Nested mutations?

I don't think this is needed. The user can setup CASCADE DELETE or use an AFTER DELETE trigger to ensure consistency of the linking table. In future, we might consider an option to somehow enable them to specify if they want to peform the deletions in the linking table as well, in which case we can figure out some logic to maintain consistency. However, this for sure should not be an immediate concern.

@severussundar
Copy link
Contributor

As outlined in the previous comments, when CASCADE DELETE/CASCADE UPDATE or triggers are setup, the foreign key integrity is taken care of.

To support the same via a GraphQL operation through DAB without requiring users to setup triggers or cascade operations,

  1. Update: DAB needs to support nested/multiple update feature where updating a Book + Author is possible.
  2. Delete: For delete, the use-case can be the following. Let's say a Book is associated with 3 Authors name Author1, Author2, Author3. The user wants to remove the link between Book and Author3, but not delete Author3 as it can have associations with other Book items. To support this, DAB has to build a net new delete mutation operation where schema defines a way to accept inputs in the linking table; also query generation logic needs to be updated to understand and process such a delete mutation.

@JerryNixon JerryNixon added the move-to-close Lacks clarity or roadmap mismatch label Aug 31, 2024
@JerryNixon
Copy link
Contributor

Current requirement is entities must be in config to be modified. The requirement for table mod is met this way, even if the case of this issue is not. For now, we will keep mod ops limited to entities actually in the config file.

@Aniruddh25 Aniruddh25 closed this as not planned Won't fix, can't repro, duplicate, stale Sep 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request move-to-close Lacks clarity or roadmap mismatch mssql pgsql mysql an issue that applies to all relational databases, same as labeling with `mssql` `mysql` and `pgsql`
Projects
None yet
Development

No branches or pull requests

5 participants