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

Error in a method findForeignKeys() of MS SQL schema #4813

Closed
SerjRamone opened this issue Aug 25, 2014 · 8 comments
Closed

Error in a method findForeignKeys() of MS SQL schema #4813

SerjRamone opened this issue Aug 25, 2014 · 8 comments
Assignees
Milestone

Comments

@SerjRamone
Copy link
Contributor

In yii2/db/mssql/Schema.php on line 347 we see a [rc].[constraint_name]

SELECT
    [kcu1].[column_name] AS [fk_column_name],
    [kcu2].[table_name] AS [uq_table_name],
    [kcu2].[column_name] AS [uq_column_name]
FROM {$referentialConstraintsTableName} AS [rc]
JOIN {$keyColumnUsageTableName} AS [kcu1] ON
    [kcu1].[constraint_catalog] = [rc].[constraint_catalog] AND
    [kcu1].[constraint_schema] = [rc].[constraint_schema] AND
    [kcu1].[constraint_name] = [rc].[constraint_name]
JOIN {$keyColumnUsageTableName} AS [kcu2] ON
    [kcu2].[constraint_catalog] = [rc].[constraint_catalog] AND
    [kcu2].[constraint_schema] = [rc].[constraint_schema] AND
    [kcu2].[constraint_name] = [rc].[constraint_name] AND
    [kcu2].[ordinal_position] = [kcu1].[ordinal_position]
WHERE [kcu1].[table_name] = :tableName

This is a mistake.
Must be a [rc].[unique_constraint_name] for get a right foreign keys.

@qiangxue qiangxue added this to the 2.0.1 milestone Aug 25, 2014
@o-rey
Copy link
Contributor

o-rey commented Aug 26, 2014

I confirm.
This issue results in incorrect relations generated by Gii.

@samdark
Copy link
Member

samdark commented Aug 26, 2014

@o-rey can you confirm that the fix suggested is correct?

@o-rey
Copy link
Contributor

o-rey commented Aug 26, 2014

Yes, at least for SQL Server 2014.

For example, if I have a table tbl_address with FK company_id pointing to tbl_company, the result of [kcu2].[constraint_name] = [rc].[constraint_name] would be

company_id  tbl_address company_id

This leads to incorrect relations generated.

Meanwhile the result of [kcu2].[constraint_name] = [rc].[unique_constraint_name] is

company_id  tbl_company id

which is correct.

@samdark
Copy link
Member

samdark commented Aug 26, 2014

Do you have a chance to test older MSSQL?

@o-rey
Copy link
Contributor

o-rey commented Aug 26, 2014

@samdark SQL 2008 confirmed.

@samdark
Copy link
Member

samdark commented Aug 26, 2014

@o-rey thanks! I've created a pull request. Since I don't have proper MSSQL environment currently it would be great if you'll verify it's good to be merged.

@o-rey
Copy link
Contributor

o-rey commented Aug 26, 2014

@samdark No, that's incorrect.
First join must not be changed.

Here's the correct query:

SELECT
    [kcu1].[column_name] AS [fk_column_name],
    [kcu2].[table_name] AS [uq_table_name],
    [kcu2].[column_name] AS [uq_column_name]
FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS AS [rc]
JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS [kcu1] ON
    [kcu1].[constraint_catalog] = [rc].[constraint_catalog] AND
    [kcu1].[constraint_schema] = [rc].[constraint_schema] AND
    [kcu1].[constraint_name] = [rc].[constraint_name]
JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS [kcu2] ON
    [kcu2].[constraint_catalog] = [rc].[constraint_catalog] AND
    [kcu2].[constraint_schema] = [rc].[constraint_schema] AND
    [kcu2].[constraint_name] = [rc].[unique_constraint_name] AND
    [kcu2].[ordinal_position] = [kcu1].[ordinal_position]
WHERE [kcu1].[table_name] = :tableName

@samdark
Copy link
Member

samdark commented Aug 26, 2014

Fixed. Thanks for reporting and verifying it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants