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

Commit e8d57201 by Manan Vaghasiya Committed by Sushant

fix(model): use schema when generating foreign keys (#9255)

1 parent 6dcc3e7d
...@@ -801,7 +801,7 @@ class Model { ...@@ -801,7 +801,7 @@ class Model {
} }
if (_.get(attribute, 'references.model.prototype') instanceof Model) { if (_.get(attribute, 'references.model.prototype') instanceof Model) {
attribute.references.model = attribute.references.model.tableName; attribute.references.model = attribute.references.model.getTableName();
} }
return attribute; return attribute;
......
...@@ -152,6 +152,43 @@ describe(Support.getTestDialectTeaser('BelongsTo'), () => { ...@@ -152,6 +152,43 @@ describe(Support.getTestDialectTeaser('BelongsTo'), () => {
}); });
}); });
}); });
it('supports schemas when defining custom foreign key attribute #9029', function() {
const User = this.sequelize.define('UserXYZ', {
uid: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
allowNull: false
}
}).schema('archive')
, Task = this.sequelize.define('TaskXYZ', {
user_id: {
type: Sequelize.INTEGER,
references: { model: User, key: 'uid' }
}
}).schema('archive');
Task.belongsTo(User, { foreignKey: 'user_id'});
return this.sequelize.dropAllSchemas().then(() => {
return this.sequelize.createSchema('archive');
}).then(() => {
return User.sync({force: true });
}).then(() => {
return Task.sync({force: true });
}).then(() => {
return User.create({});
}).then(user => {
return Task.create({}).then(task => {
return task.setUserXYZ(user).then(() => {
return task.getUserXYZ();
});
});
}).then(user => {
expect(user).to.be.ok;
});
});
}); });
describe('setAssociation', () => { describe('setAssociation', () => {
......
...@@ -25,6 +25,30 @@ describe(Support.getTestDialectTeaser('SQL'), () => { ...@@ -25,6 +25,30 @@ describe(Support.getTestDialectTeaser('SQL'), () => {
}); });
}); });
}); });
describe('with references', () => {
const BarUser = current.define('user', {
timestamps: false
}).schema('bar');
const BarProject = current.define('project', {
user_id: {
type: DataTypes.INTEGER,
references: { model: BarUser },
onUpdate: 'CASCADE',
onDelete: 'NO ACTION'
}
}, {
timestamps: false
}).schema('bar');
BarProject.belongsTo(BarUser, { foreignKey: 'user_id' });
it('references right schema when adding foreign key #9029', () => {
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);',
postgres: 'CREATE TABLE IF NOT EXISTS "bar"."projects" ("id" SERIAL , "user_id" INTEGER REFERENCES "bar"."users" ("id") ON DELETE NO ACTION ON UPDATE CASCADE, PRIMARY KEY ("id"));',
mysql: 'CREATE TABLE IF NOT EXISTS `bar.projects` (`id` INTEGER NOT NULL auto_increment , `user_id` INTEGER, PRIMARY KEY (`id`), FOREIGN KEY (`user_id`) REFERENCES `bar.users` (`id`) ON DELETE NO ACTION ON UPDATE CASCADE) ENGINE=InnoDB;',
mssql: 'IF OBJECT_ID(\'[bar].[projects]\', \'U\') IS NULL CREATE TABLE [bar].[projects] ([id] INTEGER NOT NULL IDENTITY(1,1) , [user_id] INTEGER NULL, PRIMARY KEY ([id]), FOREIGN KEY ([user_id]) REFERENCES [bar].[users] ([id]) ON DELETE NO ACTION);'
});
});
});
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!