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

Commit 10029e1f by Sascha Depold

Make `references` an object

1 parent fbf4cee8
# Next
- [CHANGED] The `references` property of model attributes has been transformed to an object: `{type: Sequelize.INTEGER, references: { model: SomeModel, key: 'some_key' }}`. The former format is deprecated.
# 3.0.0 # 3.0.0
3.0.0 cleans up a lot of deprecated code, making it easier for us to develop and maintain features in the future. 3.0.0 cleans up a lot of deprecated code, making it easier for us to develop and maintain features in the future.
......
...@@ -590,8 +590,10 @@ Series = sequelize.define('Series', { ...@@ -590,8 +590,10 @@ Series = sequelize.define('Series', {
// Set FK relationship (hasMany) with `Trainer` // Set FK relationship (hasMany) with `Trainer`
trainer_id: { trainer_id: {
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
references: "Trainers", references: {
referencesKey: "id" model: "Trainers",
key: "id"
}
} }
}) })
   
...@@ -609,8 +611,10 @@ Video = sequelize.define('Video', { ...@@ -609,8 +611,10 @@ Video = sequelize.define('Video', {
// set relationship (hasOne) with `Series` // set relationship (hasOne) with `Series`
series_id: { series_id: {
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
references: Series, // Can be both a string representing the table name, or a reference to the model references: {
referencesKey: "id" model: Series, // Can be both a string representing the table name, or a reference to the model
key: "id"
}
} }
}); });
   
......
...@@ -15,8 +15,10 @@ module.exports = function(sequelize, DataTypes) { ...@@ -15,8 +15,10 @@ module.exports = function(sequelize, DataTypes) {
// Set FK relationship (hasMany) with `Trainer` // Set FK relationship (hasMany) with `Trainer`
trainer_id: { trainer_id: {
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
references: "Trainer", references: {
referencesKey: 'id' model: 'Trainer',
key: 'id'
}
} }
}, { }, {
// don't need timestamp attributes for this model // don't need timestamp attributes for this model
......
...@@ -15,8 +15,10 @@ module.exports = function(sequelize, DataTypes) { ...@@ -15,8 +15,10 @@ module.exports = function(sequelize, DataTypes) {
// set relationship (hasOne) with `Series` // set relationship (hasOne) with `Series`
series_id: { series_id: {
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
references: "Series", references: {
referencesKey: 'id' model: 'Series',
key: 'id'
}
} }
}, { }, {
// don't need timestamp attributes for this model // don't need timestamp attributes for this model
......
...@@ -225,8 +225,10 @@ module.exports = (function() { ...@@ -225,8 +225,10 @@ module.exports = (function() {
} }
if (this.options.constraints !== false) { if (this.options.constraints !== false) {
sourceAttribute.references = this.source.getTableName(); sourceAttribute.references = {
sourceAttribute.referencesKey = sourceKeyField; model: this.source.getTableName(),
key: sourceKeyField
};
// For the source attribute the passed option is the priority // For the source attribute the passed option is the priority
sourceAttribute.onDelete = this.options.onDelete || this.through.model.rawAttributes[this.identifier].onDelete; sourceAttribute.onDelete = this.options.onDelete || this.through.model.rawAttributes[this.identifier].onDelete;
sourceAttribute.onUpdate = this.options.onUpdate || this.through.model.rawAttributes[this.identifier].onUpdate; sourceAttribute.onUpdate = this.options.onUpdate || this.through.model.rawAttributes[this.identifier].onUpdate;
...@@ -234,9 +236,10 @@ module.exports = (function() { ...@@ -234,9 +236,10 @@ module.exports = (function() {
if (!sourceAttribute.onDelete) sourceAttribute.onDelete = 'CASCADE'; if (!sourceAttribute.onDelete) sourceAttribute.onDelete = 'CASCADE';
if (!sourceAttribute.onUpdate) sourceAttribute.onUpdate = 'CASCADE'; if (!sourceAttribute.onUpdate) sourceAttribute.onUpdate = 'CASCADE';
targetAttribute.references = {
targetAttribute.references = this.target.getTableName(); model: this.target.getTableName(),
targetAttribute.referencesKey = targetKeyField; key: targetKeyField
};
// But the for target attribute the previously defined option is the priority (since it could've been set by another belongsToMany call) // But the for target attribute the previously defined option is the priority (since it could've been set by another belongsToMany call)
targetAttribute.onDelete = this.through.model.rawAttributes[this.foreignIdentifier].onDelete || this.options.onDelete; targetAttribute.onDelete = this.through.model.rawAttributes[this.foreignIdentifier].onDelete || this.options.onDelete;
targetAttribute.onUpdate = this.through.model.rawAttributes[this.foreignIdentifier].onUpdate || this.options.onUpdate; targetAttribute.onUpdate = this.through.model.rawAttributes[this.foreignIdentifier].onUpdate || this.options.onUpdate;
......
...@@ -26,18 +26,20 @@ module.exports = { ...@@ -26,18 +26,20 @@ module.exports = {
if (primaryKeys.length === 1) { if (primaryKeys.length === 1) {
if (!!source.options.schema) { if (!!source.options.schema) {
newAttribute.references = source.modelManager.sequelize.queryInterface.QueryGenerator.addSchema({ newAttribute.references = {
model: source.modelManager.sequelize.queryInterface.QueryGenerator.addSchema({
tableName: source.tableName, tableName: source.tableName,
options: { options: {
schema: source.options.schema, schema: source.options.schema,
schemaDelimiter: source.options.schemaDelimiter schemaDelimiter: source.options.schemaDelimiter
} }
}); })
};
} else { } else {
newAttribute.references = source.tableName; newAttribute.references = { model: source.tableName };
} }
newAttribute.referencesKey = primaryKeys[0]; newAttribute.references.key = primaryKeys[0];
newAttribute.onDelete = options.onDelete; newAttribute.onDelete = options.onDelete;
newAttribute.onUpdate = options.onUpdate; newAttribute.onUpdate = options.onUpdate;
} }
......
...@@ -333,7 +333,7 @@ module.exports = (function() { ...@@ -333,7 +333,7 @@ module.exports = (function() {
} }
// handle self referential constraints // handle self referential constraints
if (attribute.Model && attribute.Model.tableName === attribute.references) { if (attribute.Model && attribute.references && attribute.Model.tableName === attribute.references.model) {
this.sequelize.log('MSSQL does not support self referencial constraints, ' this.sequelize.log('MSSQL does not support self referencial constraints, '
+ 'we will remove it but we recommend restructuring your query'); + 'we will remove it but we recommend restructuring your query');
attribute.onDelete = ''; attribute.onDelete = '';
...@@ -380,10 +380,10 @@ module.exports = (function() { ...@@ -380,10 +380,10 @@ module.exports = (function() {
} }
if (attribute.references) { if (attribute.references) {
template += ' REFERENCES ' + this.quoteTable(attribute.references); template += ' REFERENCES ' + this.quoteTable(attribute.references.model);
if (attribute.referencesKey) { if (attribute.references.key) {
template += ' (' + this.quoteIdentifier(attribute.referencesKey) + ')'; template += ' (' + this.quoteIdentifier(attribute.references.key) + ')';
} else { } else {
template += ' (' + this.quoteIdentifier('id') + ')'; template += ' (' + this.quoteIdentifier('id') + ')';
} }
...@@ -410,12 +410,12 @@ module.exports = (function() { ...@@ -410,12 +410,12 @@ module.exports = (function() {
attribute = attributes[key]; attribute = attributes[key];
if (attribute.references) { if (attribute.references) {
if (existingConstraints.indexOf(attribute.references.toString()) !== -1) { if (existingConstraints.indexOf(attribute.references.model.toString()) !== -1) {
// no cascading constraints to a table more than once // no cascading constraints to a table more than once
attribute.onDelete = ''; attribute.onDelete = '';
attribute.onUpdate = ''; attribute.onUpdate = '';
} else { } else {
existingConstraints.push(attribute.references.toString()); existingConstraints.push(attribute.references.model.toString());
// NOTE: this really just disables cascading updates for all // NOTE: this really just disables cascading updates for all
// definitions. Can be made more robust to support the // definitions. Can be made more robust to support the
......
...@@ -292,10 +292,10 @@ module.exports = (function() { ...@@ -292,10 +292,10 @@ module.exports = (function() {
} }
if (attribute.references) { if (attribute.references) {
template += ' REFERENCES ' + this.quoteTable(attribute.references); template += ' REFERENCES ' + this.quoteTable(attribute.references.model);
if (attribute.referencesKey) { if (attribute.references.key) {
template += ' (' + this.quoteIdentifier(attribute.referencesKey) + ')'; template += ' (' + this.quoteIdentifier(attribute.references.key) + ')';
} else { } else {
template += ' (' + this.quoteIdentifier('id') + ')'; template += ' (' + this.quoteIdentifier('id') + ')';
} }
......
...@@ -511,10 +511,10 @@ module.exports = (function() { ...@@ -511,10 +511,10 @@ module.exports = (function() {
if (attribute.references) { if (attribute.references) {
template += ' REFERENCES <%= referencesTable %> (<%= referencesKey %>)'; template += ' REFERENCES <%= referencesTable %> (<%= referencesKey %>)';
replacements.referencesTable = this.quoteTable(attribute.references); replacements.referencesTable = this.quoteTable(attribute.references.model);
if (attribute.referencesKey) { if (attribute.references.key) {
replacements.referencesKey = this.quoteIdentifiers(attribute.referencesKey); replacements.referencesKey = this.quoteIdentifiers(attribute.references.key);
} else { } else {
replacements.referencesKey = this.quoteIdentifier('id'); replacements.referencesKey = this.quoteIdentifier('id');
} }
......
...@@ -252,10 +252,10 @@ module.exports = (function() { ...@@ -252,10 +252,10 @@ module.exports = (function() {
if(dataType.references) { if(dataType.references) {
template += ' REFERENCES <%= referencesTable %> (<%= referencesKey %>)'; template += ' REFERENCES <%= referencesTable %> (<%= referencesKey %>)';
replacements.referencesTable = this.quoteTable(dataType.references); replacements.referencesTable = this.quoteTable(dataType.references.model);
if(dataType.referencesKey) { if(dataType.references.key) {
replacements.referencesKey = this.quoteIdentifier(dataType.referencesKey); replacements.referencesKey = this.quoteIdentifier(dataType.references.key);
} else { } else {
replacements.referencesKey = this.quoteIdentifier('id'); replacements.referencesKey = this.quoteIdentifier('id');
} }
......
...@@ -68,11 +68,12 @@ module.exports = (function() { ...@@ -68,11 +68,12 @@ module.exports = (function() {
for (var attrName in model.rawAttributes) { for (var attrName in model.rawAttributes) {
if (model.rawAttributes.hasOwnProperty(attrName)) { if (model.rawAttributes.hasOwnProperty(attrName)) {
if (model.rawAttributes[attrName].references) { if (model.rawAttributes[attrName].references) {
dep = model.rawAttributes[attrName].references; dep = model.rawAttributes[attrName].references.model;
if (_.isObject(dep)) { if (_.isObject(dep)) {
dep = dep.schema + '.' + dep.tableName; dep = dep.schema + '.' + dep.tableName;
} }
deps.push(dep); deps.push(dep);
} }
} }
......
...@@ -74,8 +74,8 @@ module.exports = (function() { ...@@ -74,8 +74,8 @@ module.exports = (function() {
} }
attribute = this.sequelize.normalizeAttribute(attribute); attribute = this.sequelize.normalizeAttribute(attribute);
if (attribute.references instanceof Model) { if (attribute.references && (attribute.references.model instanceof Model)) {
attribute.references = attribute.references.tableName; attribute.references.model = attribute.references.model.tableName;
} }
if (attribute.type === undefined) { if (attribute.type === undefined) {
......
...@@ -456,8 +456,9 @@ module.exports = (function() { ...@@ -456,8 +456,9 @@ module.exports = (function() {
* @param {String} [attributes.column.field=null] If set, sequelize will map the attribute name to a different name in the database * @param {String} [attributes.column.field=null] If set, sequelize will map the attribute name to a different name in the database
* @param {Boolean} [attributes.column.autoIncrement=false] * @param {Boolean} [attributes.column.autoIncrement=false]
* @param {String} [attributes.column.comment=null] * @param {String} [attributes.column.comment=null]
* @param {String|Model} [attributes.column.references] If this column references another table, provide it here as a Model, or a string * @param {String|Model} [attributes.column.references=null] An object with reference configurations
* @param {String} [attributes.column.referencesKey='id'] The column of the foreign table that this column references * @param {String|Model} [attributes.column.references.model] If this column references another table, provide it here as a Model, or a string
* @param {String} [attributes.column.references.key='id'] The column of the foreign table that this column references
* @param {String} [attributes.column.onUpdate] What should happen when the referenced key is updated. One of CASCADE, RESTRICT, SET DEFAULT, SET NULL or NO ACTION * @param {String} [attributes.column.onUpdate] What should happen when the referenced key is updated. One of CASCADE, RESTRICT, SET DEFAULT, SET NULL or NO ACTION
* @param {String} [attributes.column.onDelete] What should happen when the referenced key is deleted. One of CASCADE, RESTRICT, SET DEFAULT, SET NULL or NO ACTION * @param {String} [attributes.column.onDelete] What should happen when the referenced key is deleted. One of CASCADE, RESTRICT, SET DEFAULT, SET NULL or NO ACTION
* @param {Function} [attributes.column.get] Provide a custom getter for this column. Use `this.getDataValue(String)` to manipulate the underlying values. * @param {Function} [attributes.column.get] Provide a custom getter for this column. Use `this.getDataValue(String)` to manipulate the underlying values.
......
...@@ -1885,8 +1885,8 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), function() { ...@@ -1885,8 +1885,8 @@ describe(Support.getTestDialectTeaser('BelongsToMany'), function() {
var UserProjects = User.belongsToMany(Project, { foreignKey: { name: 'user_id', defaultValue: 42 }, through: 'UserProjects' }); var UserProjects = User.belongsToMany(Project, { foreignKey: { name: 'user_id', defaultValue: 42 }, through: 'UserProjects' });
expect(UserProjects.through.model.rawAttributes.user_id).to.be.ok; expect(UserProjects.through.model.rawAttributes.user_id).to.be.ok;
expect(UserProjects.through.model.rawAttributes.user_id.references).to.equal(User.getTableName()); expect(UserProjects.through.model.rawAttributes.user_id.references.model).to.equal(User.getTableName());
expect(UserProjects.through.model.rawAttributes.user_id.referencesKey).to.equal('uid'); expect(UserProjects.through.model.rawAttributes.user_id.references.key).to.equal('uid');
expect(UserProjects.through.model.rawAttributes.user_id.defaultValue).to.equal(42); expect(UserProjects.through.model.rawAttributes.user_id.defaultValue).to.equal(42);
}); });
}); });
......
...@@ -637,8 +637,8 @@ describe(Support.getTestDialectTeaser('BelongsTo'), function() { ...@@ -637,8 +637,8 @@ describe(Support.getTestDialectTeaser('BelongsTo'), function() {
expect(Task.rawAttributes.uid).to.be.ok; expect(Task.rawAttributes.uid).to.be.ok;
expect(Task.rawAttributes.uid.allowNull).to.be.false; expect(Task.rawAttributes.uid.allowNull).to.be.false;
expect(Task.rawAttributes.uid.references).to.equal(User.getTableName()); expect(Task.rawAttributes.uid.references.model).to.equal(User.getTableName());
expect(Task.rawAttributes.uid.referencesKey).to.equal('id'); expect(Task.rawAttributes.uid.references.key).to.equal('id');
}); });
it('works when taking a column directly from the object', function() { it('works when taking a column directly from the object', function() {
...@@ -658,8 +658,8 @@ describe(Support.getTestDialectTeaser('BelongsTo'), function() { ...@@ -658,8 +658,8 @@ describe(Support.getTestDialectTeaser('BelongsTo'), function() {
Profile.belongsTo(User, { foreignKey: Profile.rawAttributes.user_id}); Profile.belongsTo(User, { foreignKey: Profile.rawAttributes.user_id});
expect(Profile.rawAttributes.user_id).to.be.ok; expect(Profile.rawAttributes.user_id).to.be.ok;
expect(Profile.rawAttributes.user_id.references).to.equal(User.getTableName()); expect(Profile.rawAttributes.user_id.references.model).to.equal(User.getTableName());
expect(Profile.rawAttributes.user_id.referencesKey).to.equal('uid'); expect(Profile.rawAttributes.user_id.references.key).to.equal('uid');
expect(Profile.rawAttributes.user_id.allowNull).to.be.false; expect(Profile.rawAttributes.user_id.allowNull).to.be.false;
}); });
......
...@@ -907,8 +907,8 @@ describe(Support.getTestDialectTeaser('HasMany'), function() { ...@@ -907,8 +907,8 @@ describe(Support.getTestDialectTeaser('HasMany'), function() {
expect(Task.rawAttributes.uid).to.be.ok; expect(Task.rawAttributes.uid).to.be.ok;
expect(Task.rawAttributes.uid.allowNull).to.be.false; expect(Task.rawAttributes.uid.allowNull).to.be.false;
expect(Task.rawAttributes.uid.references).to.equal(User.getTableName()); expect(Task.rawAttributes.uid.references.model).to.equal(User.getTableName());
expect(Task.rawAttributes.uid.referencesKey).to.equal('id'); expect(Task.rawAttributes.uid.references.key).to.equal('id');
}); });
it('works when taking a column directly from the object', function() { it('works when taking a column directly from the object', function() {
...@@ -928,8 +928,8 @@ describe(Support.getTestDialectTeaser('HasMany'), function() { ...@@ -928,8 +928,8 @@ describe(Support.getTestDialectTeaser('HasMany'), function() {
User.hasMany(Project, { foreignKey: Project.rawAttributes.user_id}); User.hasMany(Project, { foreignKey: Project.rawAttributes.user_id});
expect(Project.rawAttributes.user_id).to.be.ok; expect(Project.rawAttributes.user_id).to.be.ok;
expect(Project.rawAttributes.user_id.references).to.equal(User.getTableName()); expect(Project.rawAttributes.user_id.references.model).to.equal(User.getTableName());
expect(Project.rawAttributes.user_id.referencesKey).to.equal('uid'); expect(Project.rawAttributes.user_id.references.key).to.equal('uid');
expect(Project.rawAttributes.user_id.defaultValue).to.equal(42); expect(Project.rawAttributes.user_id.defaultValue).to.equal(42);
}); });
......
...@@ -530,8 +530,8 @@ describe(Support.getTestDialectTeaser('HasOne'), function() { ...@@ -530,8 +530,8 @@ describe(Support.getTestDialectTeaser('HasOne'), function() {
}); });
expect(Profile.rawAttributes.uid).to.be.ok; expect(Profile.rawAttributes.uid).to.be.ok;
expect(Profile.rawAttributes.uid.references).to.equal(User.getTableName()); expect(Profile.rawAttributes.uid.references.model).to.equal(User.getTableName());
expect(Profile.rawAttributes.uid.referencesKey).to.equal('id'); expect(Profile.rawAttributes.uid.references.key).to.equal('id');
expect(Profile.rawAttributes.uid.allowNull).to.be.false; expect(Profile.rawAttributes.uid.allowNull).to.be.false;
// Let's clear it // Let's clear it
...@@ -544,8 +544,8 @@ describe(Support.getTestDialectTeaser('HasOne'), function() { ...@@ -544,8 +544,8 @@ describe(Support.getTestDialectTeaser('HasOne'), function() {
}); });
expect(Profile.rawAttributes.uid).to.be.ok; expect(Profile.rawAttributes.uid).to.be.ok;
expect(Profile.rawAttributes.uid.references).to.equal(User.getTableName()); expect(Profile.rawAttributes.uid.references.model).to.equal(User.getTableName());
expect(Profile.rawAttributes.uid.referencesKey).to.equal('id'); expect(Profile.rawAttributes.uid.references.key).to.equal('id');
expect(Profile.rawAttributes.uid.allowNull).to.be.false; expect(Profile.rawAttributes.uid.allowNull).to.be.false;
}); });
...@@ -566,8 +566,8 @@ describe(Support.getTestDialectTeaser('HasOne'), function() { ...@@ -566,8 +566,8 @@ describe(Support.getTestDialectTeaser('HasOne'), function() {
User.hasOne(Profile, { foreignKey: Profile.rawAttributes.user_id}); User.hasOne(Profile, { foreignKey: Profile.rawAttributes.user_id});
expect(Profile.rawAttributes.user_id).to.be.ok; expect(Profile.rawAttributes.user_id).to.be.ok;
expect(Profile.rawAttributes.user_id.references).to.equal(User.getTableName()); expect(Profile.rawAttributes.user_id.references.model).to.equal(User.getTableName());
expect(Profile.rawAttributes.user_id.referencesKey).to.equal('uid'); expect(Profile.rawAttributes.user_id.references.key).to.equal('uid');
expect(Profile.rawAttributes.user_id.allowNull).to.be.false; expect(Profile.rawAttributes.user_id.allowNull).to.be.false;
}); });
...@@ -589,8 +589,8 @@ describe(Support.getTestDialectTeaser('HasOne'), function() { ...@@ -589,8 +589,8 @@ describe(Support.getTestDialectTeaser('HasOne'), function() {
expect(Project.rawAttributes.userUid).to.be.ok; expect(Project.rawAttributes.userUid).to.be.ok;
expect(Project.rawAttributes.userUid.allowNull).to.be.false; expect(Project.rawAttributes.userUid.allowNull).to.be.false;
expect(Project.rawAttributes.userUid.references).to.equal(User.getTableName()); expect(Project.rawAttributes.userUid.references.model).to.equal(User.getTableName());
expect(Project.rawAttributes.userUid.referencesKey).to.equal('uid'); expect(Project.rawAttributes.userUid.references.key).to.equal('uid');
expect(Project.rawAttributes.userUid.defaultValue).to.equal(42); expect(Project.rawAttributes.userUid.defaultValue).to.equal(42);
}); });
}); });
......
...@@ -44,23 +44,23 @@ if (Support.dialectIsMySQL()) { ...@@ -44,23 +44,23 @@ if (Support.dialectIsMySQL()) {
expectation: {id: 'INTEGER UNIQUE'} expectation: {id: 'INTEGER UNIQUE'}
}, },
{ {
arguments: [{id: {type: 'INTEGER', references: 'Bar'}}], arguments: [{id: {type: 'INTEGER', references: { model: 'Bar' }}}],
expectation: {id: 'INTEGER REFERENCES `Bar` (`id`)'} expectation: {id: 'INTEGER REFERENCES `Bar` (`id`)'}
}, },
{ {
arguments: [{id: {type: 'INTEGER', references: 'Bar', referencesKey: 'pk'}}], arguments: [{id: {type: 'INTEGER', references: { model: 'Bar', key: 'pk' }}}],
expectation: {id: 'INTEGER REFERENCES `Bar` (`pk`)'} expectation: {id: 'INTEGER REFERENCES `Bar` (`pk`)'}
}, },
{ {
arguments: [{id: {type: 'INTEGER', references: 'Bar', onDelete: 'CASCADE'}}], arguments: [{id: {type: 'INTEGER', references: { model: 'Bar' }, onDelete: 'CASCADE'}}],
expectation: {id: 'INTEGER REFERENCES `Bar` (`id`) ON DELETE CASCADE'} expectation: {id: 'INTEGER REFERENCES `Bar` (`id`) ON DELETE CASCADE'}
}, },
{ {
arguments: [{id: {type: 'INTEGER', references: 'Bar', onUpdate: 'RESTRICT'}}], arguments: [{id: {type: 'INTEGER', references: { model: 'Bar' }, onUpdate: 'RESTRICT'}}],
expectation: {id: 'INTEGER REFERENCES `Bar` (`id`) ON UPDATE RESTRICT'} expectation: {id: 'INTEGER REFERENCES `Bar` (`id`) ON UPDATE RESTRICT'}
}, },
{ {
arguments: [{id: {type: 'INTEGER', allowNull: false, autoIncrement: true, defaultValue: 1, references: 'Bar', onDelete: 'CASCADE', onUpdate: 'RESTRICT'}}], arguments: [{id: {type: 'INTEGER', allowNull: false, autoIncrement: true, defaultValue: 1, references: { model: 'Bar' }, onDelete: 'CASCADE', onUpdate: 'RESTRICT'}}],
expectation: {id: 'INTEGER NOT NULL auto_increment DEFAULT 1 REFERENCES `Bar` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT'} expectation: {id: 'INTEGER NOT NULL auto_increment DEFAULT 1 REFERENCES `Bar` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT'}
} }
], ],
......
...@@ -58,49 +58,49 @@ if (dialect.match(/^postgres/)) { ...@@ -58,49 +58,49 @@ if (dialect.match(/^postgres/)) {
expectation: {id: 'INTEGER UNIQUE'} expectation: {id: 'INTEGER UNIQUE'}
}, },
{ {
arguments: [{id: {type: 'INTEGER', references: 'Bar'}}], arguments: [{id: {type: 'INTEGER', references: { model: 'Bar' }}}],
expectation: {id: 'INTEGER REFERENCES "Bar" ("id")'} expectation: {id: 'INTEGER REFERENCES "Bar" ("id")'}
}, },
{ {
arguments: [{id: {type: 'INTEGER', references: 'Bar', referencesKey: 'pk'}}], arguments: [{id: {type: 'INTEGER', references: { model: 'Bar', key: 'pk' }}}],
expectation: {id: 'INTEGER REFERENCES "Bar" ("pk")'} expectation: {id: 'INTEGER REFERENCES "Bar" ("pk")'}
}, },
{ {
arguments: [{id: {type: 'INTEGER', references: 'Bar', onDelete: 'CASCADE'}}], arguments: [{id: {type: 'INTEGER', references: { model: 'Bar' }, onDelete: 'CASCADE'}}],
expectation: {id: 'INTEGER REFERENCES "Bar" ("id") ON DELETE CASCADE'} expectation: {id: 'INTEGER REFERENCES "Bar" ("id") ON DELETE CASCADE'}
}, },
{ {
arguments: [{id: {type: 'INTEGER', references: 'Bar', onUpdate: 'RESTRICT'}}], arguments: [{id: {type: 'INTEGER', references: { model: 'Bar' }, onUpdate: 'RESTRICT'}}],
expectation: {id: 'INTEGER REFERENCES "Bar" ("id") ON UPDATE RESTRICT'} expectation: {id: 'INTEGER REFERENCES "Bar" ("id") ON UPDATE RESTRICT'}
}, },
{ {
arguments: [{id: {type: 'INTEGER', allowNull: false, defaultValue: 1, references: 'Bar', onDelete: 'CASCADE', onUpdate: 'RESTRICT'}}], arguments: [{id: {type: 'INTEGER', allowNull: false, defaultValue: 1, references: { model: 'Bar' }, onDelete: 'CASCADE', onUpdate: 'RESTRICT'}}],
expectation: {id: 'INTEGER NOT NULL DEFAULT 1 REFERENCES "Bar" ("id") ON DELETE CASCADE ON UPDATE RESTRICT'} expectation: {id: 'INTEGER NOT NULL DEFAULT 1 REFERENCES "Bar" ("id") ON DELETE CASCADE ON UPDATE RESTRICT'}
}, },
// Variants when quoteIdentifiers is false // Variants when quoteIdentifiers is false
{ {
arguments: [{id: {type: 'INTEGER', references: 'Bar'}}], arguments: [{id: {type: 'INTEGER', references: { model: 'Bar' }}}],
expectation: {id: 'INTEGER REFERENCES Bar (id)'}, expectation: {id: 'INTEGER REFERENCES Bar (id)'},
context: {options: {quoteIdentifiers: false}} context: {options: {quoteIdentifiers: false}}
}, },
{ {
arguments: [{id: {type: 'INTEGER', references: 'Bar', referencesKey: 'pk'}}], arguments: [{id: {type: 'INTEGER', references: { model: 'Bar', key: 'pk' }}}],
expectation: {id: 'INTEGER REFERENCES Bar (pk)'}, expectation: {id: 'INTEGER REFERENCES Bar (pk)'},
context: {options: {quoteIdentifiers: false}} context: {options: {quoteIdentifiers: false}}
}, },
{ {
arguments: [{id: {type: 'INTEGER', references: 'Bar', onDelete: 'CASCADE'}}], arguments: [{id: {type: 'INTEGER', references: { model: 'Bar' }, onDelete: 'CASCADE'}}],
expectation: {id: 'INTEGER REFERENCES Bar (id) ON DELETE CASCADE'}, expectation: {id: 'INTEGER REFERENCES Bar (id) ON DELETE CASCADE'},
context: {options: {quoteIdentifiers: false}} context: {options: {quoteIdentifiers: false}}
}, },
{ {
arguments: [{id: {type: 'INTEGER', references: 'Bar', onUpdate: 'RESTRICT'}}], arguments: [{id: {type: 'INTEGER', references: { model: 'Bar' }, onUpdate: 'RESTRICT'}}],
expectation: {id: 'INTEGER REFERENCES Bar (id) ON UPDATE RESTRICT'}, expectation: {id: 'INTEGER REFERENCES Bar (id) ON UPDATE RESTRICT'},
context: {options: {quoteIdentifiers: false}} context: {options: {quoteIdentifiers: false}}
}, },
{ {
arguments: [{id: {type: 'INTEGER', allowNull: false, defaultValue: 1, references: 'Bar', onDelete: 'CASCADE', onUpdate: 'RESTRICT'}}], arguments: [{id: {type: 'INTEGER', allowNull: false, defaultValue: 1, references: { model: 'Bar' }, onDelete: 'CASCADE', onUpdate: 'RESTRICT'}}],
expectation: {id: 'INTEGER NOT NULL DEFAULT 1 REFERENCES Bar (id) ON DELETE CASCADE ON UPDATE RESTRICT'}, expectation: {id: 'INTEGER NOT NULL DEFAULT 1 REFERENCES Bar (id) ON DELETE CASCADE ON UPDATE RESTRICT'},
context: {options: {quoteIdentifiers: false}} context: {options: {quoteIdentifiers: false}}
} }
......
...@@ -54,23 +54,23 @@ if (dialect === 'sqlite') { ...@@ -54,23 +54,23 @@ if (dialect === 'sqlite') {
expectation: {id: 'INTEGER UNIQUE'} expectation: {id: 'INTEGER UNIQUE'}
}, },
{ {
arguments: [{id: {type: 'INTEGER', references: 'Bar'}}], arguments: [{id: {type: 'INTEGER', references: { model: 'Bar' }}}],
expectation: {id: 'INTEGER REFERENCES `Bar` (`id`)'} expectation: {id: 'INTEGER REFERENCES `Bar` (`id`)'}
}, },
{ {
arguments: [{id: {type: 'INTEGER', references: 'Bar', referencesKey: 'pk'}}], arguments: [{id: {type: 'INTEGER', references: { model: 'Bar', key: 'pk' }}}],
expectation: {id: 'INTEGER REFERENCES `Bar` (`pk`)'} expectation: {id: 'INTEGER REFERENCES `Bar` (`pk`)'}
}, },
{ {
arguments: [{id: {type: 'INTEGER', references: 'Bar', onDelete: 'CASCADE'}}], arguments: [{id: {type: 'INTEGER', references: { model: 'Bar' }, onDelete: 'CASCADE'}}],
expectation: {id: 'INTEGER REFERENCES `Bar` (`id`) ON DELETE CASCADE'} expectation: {id: 'INTEGER REFERENCES `Bar` (`id`) ON DELETE CASCADE'}
}, },
{ {
arguments: [{id: {type: 'INTEGER', references: 'Bar', onUpdate: 'RESTRICT'}}], arguments: [{id: {type: 'INTEGER', references: { model: 'Bar' }, onUpdate: 'RESTRICT'}}],
expectation: {id: 'INTEGER REFERENCES `Bar` (`id`) ON UPDATE RESTRICT'} expectation: {id: 'INTEGER REFERENCES `Bar` (`id`) ON UPDATE RESTRICT'}
}, },
{ {
arguments: [{id: {type: 'INTEGER', allowNull: false, defaultValue: 1, references: 'Bar', onDelete: 'CASCADE', onUpdate: 'RESTRICT'}}], arguments: [{id: {type: 'INTEGER', allowNull: false, defaultValue: 1, references: { model: 'Bar' }, onDelete: 'CASCADE', onUpdate: 'RESTRICT'}}],
expectation: {id: 'INTEGER NOT NULL DEFAULT 1 REFERENCES `Bar` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT'} expectation: {id: 'INTEGER NOT NULL DEFAULT 1 REFERENCES `Bar` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT'}
} }
], ],
......
...@@ -1241,8 +1241,10 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() { ...@@ -1241,8 +1241,10 @@ describe(Support.getTestDialectTeaser('Includes with schemas'), function() {
}, },
UserId: { UserId: {
type: Sequelize.INTEGER, type: Sequelize.INTEGER,
references: UserModel, references: {
referencesKey: 'Id' model: UserModel,
key: 'Id'
}
}, },
Name: Sequelize.STRING, Name: Sequelize.STRING,
Contact: Sequelize.STRING, Contact: Sequelize.STRING,
......
...@@ -2183,8 +2183,10 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -2183,8 +2183,10 @@ describe(Support.getTestDialectTeaser('Model'), function() {
title: Sequelize.STRING, title: Sequelize.STRING,
authorId: { authorId: {
type: Sequelize.INTEGER, type: Sequelize.INTEGER,
references: this.Author, references: {
referencesKey: 'id' model: this.Author,
key: 'id'
}
} }
}); });
...@@ -2213,8 +2215,10 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -2213,8 +2215,10 @@ describe(Support.getTestDialectTeaser('Model'), function() {
title: Sequelize.STRING, title: Sequelize.STRING,
authorId: { authorId: {
type: Sequelize.INTEGER, type: Sequelize.INTEGER,
references: 'authors', references: {
referencesKey: 'id' model: 'authors',
key: 'id'
}
} }
}); });
...@@ -2242,8 +2246,10 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -2242,8 +2246,10 @@ describe(Support.getTestDialectTeaser('Model'), function() {
title: Sequelize.STRING, title: Sequelize.STRING,
authorId: { authorId: {
type: Sequelize.INTEGER, type: Sequelize.INTEGER,
references: '4uth0r5', references: {
referencesKey: 'id' model: '4uth0r5',
key: 'id'
}
} }
}); });
...@@ -2287,8 +2293,10 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -2287,8 +2293,10 @@ describe(Support.getTestDialectTeaser('Model'), function() {
id: { id: {
type: Sequelize.INTEGER, type: Sequelize.INTEGER,
primaryKey: true, primaryKey: true,
references: Member, references: {
referencesKey: 'id', model: Member,
key: 'id'
},
autoIncrement: false, autoIncrement: false,
comment: 'asdf' comment: 'asdf'
} }
...@@ -2571,8 +2579,10 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -2571,8 +2579,10 @@ describe(Support.getTestDialectTeaser('Model'), function() {
var project = this.sequelize.define('project', { var project = this.sequelize.define('project', {
UserId: { UserId: {
type: Sequelize.STRING, type: Sequelize.STRING,
references: 'Users', references: {
referencesKey: 'UUID' model: 'Users',
key: 'UUID'
}
} }
}); });
......
...@@ -456,8 +456,8 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -456,8 +456,8 @@ describe(Support.getTestDialectTeaser('Model'), function() {
return this.sequelize.sync({ force: true }) return this.sequelize.sync({ force: true })
.then(function() { .then(function() {
var attrs = this.Task.tableAttributes; var attrs = this.Task.tableAttributes;
expect(attrs.user_id.references).to.equal('users'); expect(attrs.user_id.references.model).to.equal('users');
expect(attrs.user_id.referencesKey).to.equal('userId'); expect(attrs.user_id.references.key).to.equal('userId');
}.bind(this)); }.bind(this));
}); });
......
...@@ -406,8 +406,10 @@ describe(Support.getTestDialectTeaser('QueryInterface'), function() { ...@@ -406,8 +406,10 @@ describe(Support.getTestDialectTeaser('QueryInterface'), function() {
}).bind(this).then(function() { }).bind(this).then(function() {
return this.queryInterface.addColumn('users', 'level_id', { return this.queryInterface.addColumn('users', 'level_id', {
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
references: 'level', references: {
referenceKey: 'id', model: 'level',
key: 'id'
},
onUpdate: 'cascade', onUpdate: 'cascade',
onDelete: 'set null' onDelete: 'set null'
}, {logging: log}); }, {logging: log});
...@@ -466,19 +468,25 @@ describe(Support.getTestDialectTeaser('QueryInterface'), function() { ...@@ -466,19 +468,25 @@ describe(Support.getTestDialectTeaser('QueryInterface'), function() {
}, },
admin: { admin: {
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
references: 'users', references: {
referenceKey: 'id' model: 'users',
key: 'id'
}
}, },
operator: { operator: {
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
references: 'users', references: {
referenceKey: 'id', model: 'users',
key: 'id'
},
onUpdate: 'cascade' onUpdate: 'cascade'
}, },
owner: { owner: {
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
references: 'users', references: {
referenceKey: 'id', model: 'users',
key: 'id'
},
onUpdate: 'cascade', onUpdate: 'cascade',
onDelete: 'set null' onDelete: 'set null'
} }
......
...@@ -737,7 +737,7 @@ describe(Support.getTestDialectTeaser('Sequelize'), function() { ...@@ -737,7 +737,7 @@ describe(Support.getTestDialectTeaser('Sequelize'), function() {
it('returns an error correctly if unable to sync a foreign key referenced model', function() { it('returns an error correctly if unable to sync a foreign key referenced model', function() {
this.sequelize.define('Application', { this.sequelize.define('Application', {
authorID: { type: Sequelize.BIGINT, allowNull: false, references: 'User', referencesKey: 'id' } authorID: { type: Sequelize.BIGINT, allowNull: false, references: { model: 'User', key: 'id' } }
}); });
return this.sequelize.sync().catch(function(error) { return this.sequelize.sync().catch(function(error) {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!