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

read_schema may add non-existent columns or foreign keys for MySQL #107

Closed
sammakkoinen opened this issue Jun 4, 2020 · 1 comment
Closed

Comments

@sammakkoinen
Copy link

In Yancy::Backend::Role::Relational, line 410, the query
my $sth = $db->dbh->foreign_key_info( undef, undef, $foreign_table, undef, undef, $table );
does not specify schema names. The resulting sql looks like:

SELECT NULL AS PKTABLE_CAT,
       A.REFERENCED_TABLE_SCHEMA AS PKTABLE_SCHEM,
       A.REFERENCED_TABLE_NAME AS PKTABLE_NAME,
       A.REFERENCED_COLUMN_NAME AS PKCOLUMN_NAME,
       A.TABLE_CATALOG AS FKTABLE_CAT,
       A.TABLE_SCHEMA AS FKTABLE_SCHEM,
       A.TABLE_NAME AS FKTABLE_NAME,
       A.COLUMN_NAME AS FKCOLUMN_NAME,
       A.ORDINAL_POSITION AS KEY_SEQ,
       NULL AS UPDATE_RULE,
       NULL AS DELETE_RULE,
       A.CONSTRAINT_NAME AS FK_NAME,
       NULL AS PK_NAME,
       NULL AS DEFERABILITY,
       NULL AS UNIQUE_OR_PRIMARY
  FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE A,
       INFORMATION_SCHEMA.TABLE_CONSTRAINTS B
 WHERE A.TABLE_SCHEMA = B.TABLE_SCHEMA AND A.TABLE_NAME = B.TABLE_NAME
   AND A.CONSTRAINT_NAME = B.CONSTRAINT_NAME AND B.CONSTRAINT_TYPE IS NOT NULL
 AND A.REFERENCED_TABLE_NAME = ? AND A.TABLE_NAME = ? ORDER BY A.TABLE_SCHEMA, A.TABLE_NAME, A.ORDINAL_POSITION

so it can select items from another database, if it has tables with the same names as REFERENCED_TABLE_NAME and TABLE_NAME. Then, in line 433:
$schema{ $table }{ properties }{ $column }{ 'x-foreign-key' } = $foreign_table;
if $column does not exist, it gets added to $schema{ $table }{ properties } by autovivification, or non-existing foreign key may be added to the existing column.
Imagine that we have two databases, both of which have tables named 'comment' and 'user', whose structure is different. We connect Yancy to database A, where these tables do not have common foreign keys. However in database B the table 'comment' has column 'author' that references user.id. This will result in adding 'author' key to the database A's schema and later
DBD::mysql::st execute failed: Unknown column 'author' in 'field list'
and internal server error when trying to open the table in the UI.

@preaction
Copy link
Owner

Agh. I should've been paying attention: The column_info and primary_key_info calls above have the correct catalog and schema arguments. I'll fix this right away, thanks for the report!

preaction added a commit that referenced this issue Jun 6, 2020
    [Added]

    - Added Yancy::I18N and started translating parts of the Yancy
      editor. Much more work to do here, but it's a start!

    [Fixed]

    - Fixed no response from the server when user tries to register
      while registration is disabled. Thanks @uniejo for the patch!
      [Github #108]
    - Fixed finding foreign keys on databases with multiple catalogs or
      schemas. Thanks @sammakkoinen for the report! [Github #107]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants