不要怂,就是干,撸起袖子干!

Commit 5dfb7188 by Sushant Committed by GitHub

fix(sqlite): unable to reference foreignKey on primaryKey (#9893)

1 parent 03155ad5
...@@ -38,8 +38,13 @@ class SQLiteQueryGenerator extends MySqlQueryGenerator { ...@@ -38,8 +38,13 @@ class SQLiteQueryGenerator extends MySqlQueryGenerator {
let dataTypeString = dataType; let dataTypeString = dataType;
if (_.includes(dataType, 'PRIMARY KEY')) { if (_.includes(dataType, 'PRIMARY KEY')) {
if (_.includes(dataType, 'INTEGER')) { // Only INTEGER is allowed for primary key, see https://github.com/sequelize/sequelize/issues/969 (no lenght, unsigned etc) if (_.includes(dataType, 'INTEGER')) {
// Only INTEGER is allowed for primary key, see https://github.com/sequelize/sequelize/issues/969 (no lenght, unsigned etc)
dataTypeString = containsAutoIncrement ? 'INTEGER PRIMARY KEY AUTOINCREMENT' : 'INTEGER PRIMARY KEY'; dataTypeString = containsAutoIncrement ? 'INTEGER PRIMARY KEY AUTOINCREMENT' : 'INTEGER PRIMARY KEY';
if (_.includes(dataType, ' REFERENCES')) {
dataTypeString += dataType.substr(dataType.indexOf(' REFERENCES'));
}
} }
if (needsMultiplePrimaryKeys) { if (needsMultiplePrimaryKeys) {
......
...@@ -15,6 +15,7 @@ describe(Support.getTestDialectTeaser('SQL'), () => { ...@@ -15,6 +15,7 @@ describe(Support.getTestDialectTeaser('SQL'), () => {
schema: 'foo', schema: 'foo',
timestamps: false timestamps: false
}); });
describe('with enums', () => { describe('with enums', () => {
it('references enum in the right schema #3171', () => { it('references enum in the right schema #3171', () => {
expectsql(sql.createTableQuery(FooUser.getTableName(), sql.attributesToSQL(FooUser.rawAttributes), { }), { expectsql(sql.createTableQuery(FooUser.getTableName(), sql.attributesToSQL(FooUser.rawAttributes), { }), {
...@@ -25,10 +26,12 @@ describe(Support.getTestDialectTeaser('SQL'), () => { ...@@ -25,10 +26,12 @@ describe(Support.getTestDialectTeaser('SQL'), () => {
}); });
}); });
}); });
describe('with references', () => { describe('with references', () => {
const BarUser = current.define('user', { const BarUser = current.define('user', {
timestamps: false timestamps: false
}).schema('bar'); }).schema('bar');
const BarProject = current.define('project', { const BarProject = current.define('project', {
user_id: { user_id: {
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
...@@ -39,7 +42,9 @@ describe(Support.getTestDialectTeaser('SQL'), () => { ...@@ -39,7 +42,9 @@ describe(Support.getTestDialectTeaser('SQL'), () => {
}, { }, {
timestamps: false timestamps: false
}).schema('bar'); }).schema('bar');
BarProject.belongsTo(BarUser, { foreignKey: 'user_id' }); BarProject.belongsTo(BarUser, { foreignKey: 'user_id' });
it('references right schema when adding foreign key #9029', () => { it('references right schema when adding foreign key #9029', () => {
expectsql(sql.createTableQuery(BarProject.getTableName(), sql.attributesToSQL(BarProject.rawAttributes), { }), { expectsql(sql.createTableQuery(BarProject.getTableName(), sql.attributesToSQL(BarProject.rawAttributes), { }), {
sqlite: 'CREATE TABLE IF NOT EXISTS `bar.projects` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `user_id` INTEGER REFERENCES `bar.users` (`id`) ON DELETE NO ACTION ON UPDATE CASCADE);', sqlite: 'CREATE TABLE IF NOT EXISTS `bar.projects` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `user_id` INTEGER REFERENCES `bar.users` (`id`) ON DELETE NO ACTION ON UPDATE CASCADE);',
...@@ -49,6 +54,33 @@ describe(Support.getTestDialectTeaser('SQL'), () => { ...@@ -49,6 +54,33 @@ describe(Support.getTestDialectTeaser('SQL'), () => {
}); });
}); });
}); });
describe('with references on primary key', () => {
const File = current.define('file', {}, { timestamps: false });
const Image = current.define('image', {
id: {
primaryKey: true,
autoIncrement: true,
type: DataTypes.INTEGER,
references: {
model: File,
key: 'id'
}
}
}, {
timestamps: false
});
it('references on primary key #9461', () => {
expectsql(sql.createTableQuery(Image.getTableName(), sql.attributesToSQL(Image.rawAttributes), { }), {
sqlite: 'CREATE TABLE IF NOT EXISTS `images` (`id` INTEGER PRIMARY KEY AUTOINCREMENT REFERENCES `files` (`id`));',
postgres: 'CREATE TABLE IF NOT EXISTS "images" ("id" SERIAL REFERENCES "files" ("id"), PRIMARY KEY ("id"));',
mysql: 'CREATE TABLE IF NOT EXISTS `images` (`id` INTEGER auto_increment , PRIMARY KEY (`id`), FOREIGN KEY (`id`) REFERENCES `files` (`id`)) ENGINE=InnoDB;',
mssql: 'IF OBJECT_ID(\'[images]\', \'U\') IS NULL CREATE TABLE [images] ([id] INTEGER IDENTITY(1,1) , PRIMARY KEY ([id]), FOREIGN KEY ([id]) REFERENCES [files] ([id]));'
});
});
});
if (current.dialect.name === 'postgres') { if (current.dialect.name === 'postgres') {
describe('IF NOT EXISTS version check', () => { describe('IF NOT EXISTS version check', () => {
const modifiedSQL = _.clone(sql); const modifiedSQL = _.clone(sql);
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!