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

addConstraint for compound primary key fails during foreign key check for queryInterface using sqlite dialect #13208

Closed
2 tasks done
wisdomadzorgenu opened this issue Apr 20, 2021 · 0 comments · Fixed by #13587

Comments

@wisdomadzorgenu
Copy link

Issue Description

This issue happens with SQLite. When I am adding a composite primary key using queryInterface.addConstraint() command, a foreign key check is performed using PRAGMA foreign_key_list(tableName).

However, the query fails when tableName is hyphenated. Ideally, any table should be accepted but it is not in this case unless I change my table name.

What are you doing?

     return queryInterface.addConstraint('calendar-list',['date','weekNumber'], {
          type: 'primary key',
          name: 'compound_calendar_list_key'
     });

What do you expect to happen?

  • Table should be created with a composite primary key.

What is actually happening?

An error was shown indicating this :

 SQL command PRAGMA foreign_key_list(calendar-list) fails. Error in the syntax near - 

Additional context

I understand the error because the command used is incorrect. It should be PRAGMA foreign_key_list('calendar-list'). Sequelize should add quotes to table names when performing foreign key check with SQLite.

I checked SQLite documentation and PRAGMA foreign_key_list('tableName') should work as it is a valid command.

I found the culprit here https://github.com/sequelize/sequelize/blob/v5/lib/dialects/sqlite/query-generator.js

 /**
   * Generates an SQL query that returns all foreign keys of a table.
   *
   * @param  {string} tableName  The name of the table.
   * @returns {string}            The generated sql query.
   * @private
   */
  getForeignKeysQuery(tableName) {
    return `PRAGMA foreign_key_list(${tableName})`;
  }

${tableName} should be in quotes '${tableName}'.

This should satisfy all scenarios for table names and shouldn't cause an issue.

Environment

  • Sequelize version: 4.x.x, 5.x.x and 6.x.x
  • Node.js version: 14.16.0
  • Operating System: MacOs Catalina 10.15.17

Issue Template Checklist

How does this problem relate to dialects?

  • I think this problem happens only for the following dialect(s): sqlite

Would you be willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time and I know how to start.
@fzn0x fzn0x linked a pull request Oct 25, 2021 that will close this issue
4 tasks
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

Successfully merging a pull request may close this issue.

1 participant