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

Fix multiple primary keys results in syntax error (duplicate "NOT NULL") #12237

Merged
merged 1 commit into from May 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/dialects/sqlite/query-generator.js
Expand Up @@ -44,7 +44,11 @@ class SQLiteQueryGenerator extends MySqlQueryGenerator {

if (needsMultiplePrimaryKeys) {
primaryKeys.push(attr);
dataTypeString = dataType.replace('PRIMARY KEY', 'NOT NULL');
if (dataType.includes('NOT NULL')) {
dataTypeString = dataType.replace(' PRIMARY KEY', '');
} else {
dataTypeString = dataType.replace('PRIMARY KEY', 'NOT NULL');
}
}
}
attrArray.push(`${this.quoteIdentifier(attr)} ${dataTypeString}`);
Expand Down
4 changes: 4 additions & 0 deletions test/unit/dialects/sqlite/query-generator.test.js
Expand Up @@ -152,6 +152,10 @@ if (dialect === 'sqlite') {
{
arguments: ['myTable', { id: 'INTEGER PRIMARY KEY AUTOINCREMENT', name: 'VARCHAR(255)', surname: 'VARCHAR(255)' }, { uniqueKeys: { uniqueConstraint: { fields: ['name', 'surname'], customIndex: true } } }],
expectation: 'CREATE TABLE IF NOT EXISTS `myTable` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `name` VARCHAR(255), `surname` VARCHAR(255), UNIQUE (`name`, `surname`));'
},
{
arguments: ['myTable', { foo1: 'INTEGER PRIMARY KEY NOT NULL', foo2: 'INTEGER PRIMARY KEY NOT NULL' }],
expectation: 'CREATE TABLE IF NOT EXISTS `myTable` (`foo1` INTEGER NOT NULL, `foo2` INTEGER NOT NULL, PRIMARY KEY (`foo1`, `foo2`));'
}
],

Expand Down