-
Notifications
You must be signed in to change notification settings - Fork 43
Foreign keys constraints #3648
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
Foreign keys constraints #3648
Conversation
a8f4b43 to
05d5935
Compare
ImeevMA
left a comment
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.
Thank you for the patch! I have a few comments, please look at them.
| -- create_parent_start | ||
| CREATE TABLE author ( | ||
| id INTEGER PRIMARY KEY, | ||
| name VARCHAR(100) NOT NULL |
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.
I suggest using STRING instead of VARCHAR(...) here and below, since our SQL does not support fixed length string values and will automatically replace them with STRING.
| Constraint Conflict Clauses | ||
| The referenced column names must be defined in the referenced table and have similar data types. | ||
| The values in the referencing columns must equal values in the referenced columns of the referenced table, | ||
| or at least one of the referencing columns must contain ``NULL``. |
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 is currently not the case. This describes MATCH SIMPLE, which is usually the default. However, we only support MATCH FULL, which requests a full match.
|
|
||
| If behavior is ROLLBACK: the statement is rolled back, and the first statement is rolled back, | ||
| and there is an error message. The table now contains nothing. | ||
| On an attempt to delete an author that already has books in the ``book`` table, the following error is raised: |
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.
The error shown is actually not what I expected from the description. This is a new limitation for FKs: referencing fields must now have an index in order to remove anything from the referenced space. Otherwise, we will not be able to remove from the referenced space, even if the row is not referenced. For example, if we add (3, 'Alexander Pushkin') to the author space and do not add anything to the book space, we won't be able to remove the added row. To remove it, we need to create an index on book.author_id.
To receive expected error I suggest to replace
box.execute([[
-- create_child_start
CREATE TABLE book (
id INTEGER PRIMARY KEY,
title VARCHAR(200) NOT NULL,
author_id INTEGER NOT NULL,
FOREIGN KEY (author_id)
REFERENCES author (id)
);
-- create_child_end
]])
by
box.execute([[
-- create_child_start
CREATE TABLE book (
id INTEGER PRIMARY KEY,
title VARCHAR(200) NOT NULL,
author_id INTEGER NOT NULL UNIQUE,
FOREIGN KEY (author_id)
REFERENCES author (id)
);
-- create_child_end
]])
and mention why we add UNIQUE constraint here.
Then the error will be:
'Foreign key ''fk_unnamed_BOOK_1'' integrity check failed: tuple is referenced'
| A foreign key constraint is defined on the child table that references the parent table's column values. | ||
|
|
||
| There is a shorthand: specifying REFERENCES in a :ref:`column definition <sql_column_def_constraint>`. | ||
| Foreign key constraints look like this: |
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.
I see that the described FK constraint is known as the FK tuple constraint. What about the FK field constraint? I'm not sure if the FK description should be duplicated due to different types of constraints, but they have different error descriptions and are described differently in '_space', so at least add a quick overview of that and links to 'tuple constraint' and ' field constraint ' looks like a good idea.
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.
Looks like this requires some investigation, created a separate ticket: #3703
05d5935 to
99a39f4
Compare
p7nov
left a comment
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.
LGTM 👍
| FOREIGN KEY constraints look like this: |br| | ||
| :samp:`FOREIGN KEY ({referencing-column-name} [, {referencing-column-name}...]) REFERENCES {referenced-table-name} [({referenced-column-name} [, {referenced-column-name}...]]) [MATCH FULL] [update-or-delete-rules]` | ||
| A foreign key is a constraint that can be used to enforce data integrity across related tables. | ||
| A foreign key constraint is defined on the child table that references the parent table's column values. |
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.
Maybe link to box foreign keys documentation? https://www.tarantool.io/en/doc/latest/concepts/data_model/value_store/#foreign-keys
| .. COMMENT | ||
| Constraint Conflict Clauses are temporarily disabled. | ||
| However, the description is here, as a big comment. | ||
| With :doc:`2.11.0 </release/2.11.0>`, the following referencing options aren't supported anymore: |
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.
Since 2.11.0?
| This example shows how to create a relation between the parent and child tables through a single-column foreign key: | ||
|
|
||
| FAIL -- return an error but do not do statement rollback. | ||
| 1. First, create a parent ``author`` table: |
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.
Nit: "first", "then", and similar words are usually redundant in procedures with numbered steps.
Let's keep it shorter
| .. _sql_foreign_key: | ||
|
|
||
| ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | ||
| Table Constraint Definition for foreign keys |
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.
99a39f4 to
37d5e04
Compare
37d5e04 to
f95a475
Compare

Updated the section about foreign key constraints: https://docs.d.tarantool.io/en/doc/sql-table-constraint/reference/reference_sql/sql_statements_and_clauses/#sql-foreign-key
Changes: