Skip to content

Add quotes around column names for unique constraints in sqlite (fixes #4407) - #7224

Merged
sushantdhiman merged 3 commits into
sequelize:masterfrom
StefanoDalpiaz:fix/sqlite-unique-quotes
Feb 19, 2017
Merged

sushantdhiman merged 3 commits into
sequelize:masterfrom
StefanoDalpiaz:fix/sqlite-unique-quotes

Conversation

@StefanoDalpiaz

Copy link
Copy Markdown
Contributor

This PR fixes a bug in the query generator for Sqlite, in which the definition of a composite unique constraint would not properly wrap in quotes the column names. This causes the statement to fail whenever a column name is the same as a reserved keyword (like order, group, index, ...), or when it contains certain non alpha-numerical characters (like a space).

For example the following declaration:

this.sequelize.define('user', {
  name: {
    type: Sequelize.STRING,
    unique: 'uniqueConstraint'
  },
  'column with space': {
    type: Sequelize.INTEGER,
    unique: 'uniqueConstraint'
  }
});

would generate the following statement (new lines added for readability):

CREATE TABLE IF NOT EXISTS `users` (
  `id` INTEGER PRIMARY KEY AUTOINCREMENT,
  `name` VARCHAR(255),
  `column with space` VARCHAR(255),
  `createdAt` DATETIME NOT NULL,
  `updatedAt` DATETIME NOT NULL,
  UNIQUE (name, column with space)
);

The above statement contains a syntax error, as the name column with space inside the UNIQUE constraint must be wrapped in quotes.

This PR modifies the logic that builds that statement to ensure that those names are properly wrapped.

I have also provided a couple of tests to prevent regression.

@StefanoDalpiaz StefanoDalpiaz changed the title Add quotes around column names for unique constraints in sqlite (#4407) Add quotes around column names for unique constraints in sqlite (fixes #4407) Feb 8, 2017
@StefanoDalpiaz
StefanoDalpiaz force-pushed the fix/sqlite-unique-quotes branch from ffb1fd7 to 6749e40 Compare February 8, 2017 06:19
@codecov-io

codecov-io commented Feb 8, 2017 •

Copy link
Copy Markdown

Codecov Report

Merging #7224 into master will not change coverage.
The diff coverage is 100%.


Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 6efc2ad...4fa374e. Read the comment docs.

@sushantdhiman

Copy link
Copy Markdown
Contributor

Rather than integration test, we should add unit test for this change, because we just want to verify correct statement was generated

@StefanoDalpiaz

Copy link
Copy Markdown
Contributor Author

As requested, I have removed the new integration tests and added a unit test.

Also, is there any chance that this can be backported into v3? I can open another PR for that if needed.

@sushantdhiman

Copy link
Copy Markdown
Contributor

Also, is there any chance that this can be backported into v3? I can open another PR for that if needed.

Please go ahead and open a PR targeting v3, we will release new version for you :)

@StefanoDalpiaz

Copy link
Copy Markdown
Contributor Author

Please go ahead and open a PR targeting v3, we will release new version for you :)

Thanks, I just opened #7270 for this purpose.

mkaufmaner pushed a commit to mkaufmaner/sequelize that referenced this pull request Apr 25, 2017
…sequelize#4407) (sequelize#7224)

* Add quotes around column names for unique constraints in sqlite (fixes sequelize#4407)

* Changelog linked to issue
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 this pull request may close these issues.

3 participants