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

Bug: incomplete join in CodeIgniter\Database\MySQLi\Connection::_foreignKeyData() #4996

Closed
lukemim opened this issue Aug 11, 2021 · 3 comments · Fixed by #5416
Closed

Bug: incomplete join in CodeIgniter\Database\MySQLi\Connection::_foreignKeyData() #4996

lukemim opened this issue Aug 11, 2021 · 3 comments · Fixed by #5416
Labels
bug Verified issues on the current code behavior or pull requests that will fix them database Issues or pull requests that affect the database layer

Comments

@lukemim
Copy link

lukemim commented Aug 11, 2021

the join in CodeIgniter\Database\MySQLi\Connection::_foreignKeyData() is joining over CONSTRAINT_NAME only, but not over CONSTRAINT_SCHEMA, so if there are multiple databases (schemas) containing constraints with the same names _foreignKeyData() will return duplicated rows with metadata also from the other databases (schemas).

I guess this is where the bug was created: 12ea7ba

CI 4.1.3

@lukemim lukemim added the bug Verified issues on the current code behavior or pull requests that will fix them label Aug 11, 2021
@MGatner MGatner added the database Issues or pull requests that affect the database layer label Aug 11, 2021
@kenjis
Copy link
Member

kenjis commented Oct 18, 2021

@kenjis
Copy link
Member

kenjis commented Nov 30, 2021

I confirmed the bug.

SELECT
    tc.CONSTRAINT_NAME,
    tc.TABLE_NAME,
    kcu.COLUMN_NAME,
    rc.REFERENCED_TABLE_NAME,
    kcu.REFERENCED_COLUMN_NAME
FROM information_schema.TABLE_CONSTRAINTS AS tc
    INNER JOIN information_schema.REFERENTIAL_CONSTRAINTS AS rc
        ON tc.CONSTRAINT_NAME = rc.CONSTRAINT_NAME
    INNER JOIN information_schema.KEY_COLUMN_USAGE AS kcu
        ON tc.CONSTRAINT_NAME = kcu.CONSTRAINT_NAME
WHERE
    tc.CONSTRAINT_TYPE = 'FOREIGN KEY' AND
        tc.TABLE_SCHEMA = 'ci4_crud' AND
        tc.TABLE_NAME = 'track';
+----------------------+----------+-----------+---------------------+----------------------+
|CONSTRAINT_NAME       |TABLE_NAME|COLUMN_NAME|REFERENCED_TABLE_NAME|REFERENCED_COLUMN_NAME|
+----------------------+----------+-----------+---------------------+----------------------+
|track_album_id_foreign|track     |album_id   |album                |id                    |
|track_album_id_foreign|track     |album_id   |album                |id                    |
|track_album_id_foreign|track     |album_id   |album                |id                    |
|track_album_id_foreign|track     |album_id   |album                |id                    |
+----------------------+----------+-----------+---------------------+----------------------+

@kenjis
Copy link
Member

kenjis commented Nov 30, 2021

SELECT
    tc.CONSTRAINT_NAME,
    tc.TABLE_NAME,
    kcu.COLUMN_NAME,
    rc.REFERENCED_TABLE_NAME,
    kcu.REFERENCED_COLUMN_NAME
FROM information_schema.TABLE_CONSTRAINTS AS tc
    INNER JOIN information_schema.REFERENTIAL_CONSTRAINTS AS rc
        ON tc.CONSTRAINT_NAME = rc.CONSTRAINT_NAME
        AND tc.CONSTRAINT_SCHEMA = rc.CONSTRAINT_SCHEMA
    INNER JOIN information_schema.KEY_COLUMN_USAGE AS kcu
        ON tc.CONSTRAINT_NAME = kcu.CONSTRAINT_NAME
        AND tc.CONSTRAINT_SCHEMA = kcu.CONSTRAINT_SCHEMA
WHERE
    tc.CONSTRAINT_TYPE = 'FOREIGN KEY' AND
        tc.TABLE_SCHEMA = 'ci4_crud' AND
        tc.TABLE_NAME = 'track';
+----------------------+----------+-----------+---------------------+----------------------+
|CONSTRAINT_NAME       |TABLE_NAME|COLUMN_NAME|REFERENCED_TABLE_NAME|REFERENCED_COLUMN_NAME|
+----------------------+----------+-----------+---------------------+----------------------+
|track_album_id_foreign|track     |album_id   |album                |id                    |
+----------------------+----------+-----------+---------------------+----------------------+

kenjis added a commit to kenjis/CodeIgniter4 that referenced this issue Nov 30, 2021
kenjis added a commit to kenjis/CodeIgniter4 that referenced this issue Dec 2, 2021
kenjis added a commit to kenjis/CodeIgniter4 that referenced this issue Dec 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them database Issues or pull requests that affect the database layer
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants