Skip to content

Commit

Permalink
Add quotes around column names for unique constraints in sqlite (fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanoDalpiaz authored and sushantdhiman committed Feb 22, 2017
1 parent 99d406e commit 6ca925b
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Future
- [FIXED] Add quotes around column names for unique constraints in sqlite [#4407](https://github.com/sequelize/sequelize/issues/4407)

# 3.30.2
- [FIXED] `previous` method gave wrong value back [#7189](https://github.com/sequelize/sequelize/pull/7189)
- [FIXED] Fixes setAssociation with scope [#7223](https://github.com/sequelize/sequelize/pull/7223)
Expand Down
3 changes: 2 additions & 1 deletion lib/dialects/sqlite/query-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@ var QueryGenerator = {
, pkString = primaryKeys.map(function(pk) { return this.quoteIdentifier(pk); }.bind(this)).join(', ');

if (!!options.uniqueKeys) {
var quoteIdentifier = this.quoteIdentifier.bind(this);
Utils._.each(options.uniqueKeys, function(columns) {
if (!columns.singleField) { // If it's a single field its handled in column def, not as an index
values.attributes += ', UNIQUE (' + columns.fields.join(', ') + ')';
values.attributes += ', UNIQUE (' + columns.fields.map(function(field) { return quoteIdentifier(field); }).join(', ') + ')';
}
});
}
Expand Down
4 changes: 4 additions & 0 deletions test/unit/dialects/sqlite/query-generator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ if (dialect === 'sqlite') {
{
arguments: ['myTable', {id: 'INTEGER PRIMARY KEY AUTOINCREMENT', name: 'VARCHAR(255)'}],
expectation: 'CREATE TABLE IF NOT EXISTS `myTable` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `name` VARCHAR(255));'
},
{
arguments: ['myTable', {id: 'INTEGER PRIMARY KEY AUTOINCREMENT', name: 'VARCHAR(255)', surname: 'VARCHAR(255)'}, {uniqueKeys: {uniqueConstraint: {fields: ['name', 'surname']}}}],
expectation: 'CREATE TABLE IF NOT EXISTS `myTable` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `name` VARCHAR(255), `surname` VARCHAR(255), UNIQUE (`name`, `surname`));'
}
],

Expand Down

0 comments on commit 6ca925b

Please sign in to comment.