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

Commit 559e1cef by Martin Aspeli

Basic implementation of capturing foreign key status

1 parent 8f861533
...@@ -24,6 +24,12 @@ module.exports = (function() { ...@@ -24,6 +24,12 @@ module.exports = (function() {
this.identifier = this.options.foreignKey || Utils._.underscoredIf(Utils.singularize(this.target.tableName) + "Id", this.source.options.underscored) this.identifier = this.options.foreignKey || Utils._.underscoredIf(Utils.singularize(this.target.tableName) + "Id", this.source.options.underscored)
newAttributes[this.identifier] = { type: DataTypes.INTEGER } newAttributes[this.identifier] = { type: DataTypes.INTEGER }
if(typeof this.options.foreignKey === "undefined" || this.options.foreignKey === true) {
newAttributes[this.identifier].references = this.target.tableName,
newAttributes[this.identifier].referencesKeys = Utils._.reduce(this.source.rawAttributes, function(memo, value, key) { if(value.primaryKey) { memo.push(key) } return memo }, [])
newAttributes[this.identifier].onDelete = this.options.onDelete,
newAttributes[this.identifier].onUpdate = this.options.onUpdate
}
Utils._.defaults(this.source.rawAttributes, newAttributes) Utils._.defaults(this.source.rawAttributes, newAttributes)
// Sync attributes to DAO proto each time a new assoc is added // Sync attributes to DAO proto each time a new assoc is added
......
...@@ -65,6 +65,12 @@ module.exports = (function() { ...@@ -65,6 +65,12 @@ module.exports = (function() {
} else { } else {
var newAttributes = {} var newAttributes = {}
newAttributes[this.identifier] = { type: DataTypes.INTEGER } newAttributes[this.identifier] = { type: DataTypes.INTEGER }
if(typeof this.options.foreignKey === "undefined" || this.options.foreignKey === true) {
newAttributes[this.identifier].references = this.source.tableName
newAttributes[this.identifier].referencesKeys = Utils._.reduce(this.source.rawAttributes, function(memo, value, key) { if(value.primaryKey) { memo.push(key) } return memo }, [])
newAttributes[this.identifier].onDelete = this.options.onDelete
newAttributes[this.identifier].onUpdate = this.options.onUpdate
}
Utils._.defaults(this.target.rawAttributes, newAttributes) Utils._.defaults(this.target.rawAttributes, newAttributes)
} }
......
...@@ -29,6 +29,12 @@ module.exports = (function() { ...@@ -29,6 +29,12 @@ module.exports = (function() {
this.identifier = this.options.foreignKey || Utils._.underscoredIf(Utils.singularize(this.source.tableName) + "Id", this.options.underscored) this.identifier = this.options.foreignKey || Utils._.underscoredIf(Utils.singularize(this.source.tableName) + "Id", this.options.underscored)
newAttributes[this.identifier] = { type: DataTypes.INTEGER } newAttributes[this.identifier] = { type: DataTypes.INTEGER }
if(typeof this.options.foreignKey === "undefined" || this.options.foreignKey === true) {
newAttributes[this.identifier].references = this.source.tableName,
newAttributes[this.identifier].referencesKeys = Utils._.reduce(this.source.rawAttributes, function(memo, value, key) { if(value.primaryKey) { memo.push(key) } return memo }, [])
newAttributes[this.identifier].onDelete = this.options.onDelete,
newAttributes[this.identifier].onUpdate = this.options.onUpdate
}
Utils._.defaults(this.target.rawAttributes, newAttributes) Utils._.defaults(this.target.rawAttributes, newAttributes)
// Sync attributes to DAO proto each time a new assoc is added // Sync attributes to DAO proto each time a new assoc is added
......
...@@ -322,5 +322,28 @@ describe(Helpers.getTestDialectTeaser("HasMany"), function() { ...@@ -322,5 +322,28 @@ describe(Helpers.getTestDialectTeaser("HasMany"), function() {
}) })
}) })
}) })
describe("Foreign key constraints", function() {
it("can cascade deletes", function(done) {
var User = this.sequelize.define('User', { username: Sequelize.STRING })
, Task = this.sequelize.define('Task', { title: Sequelize.STRING })
User.hasMany(Task, {onDelete: 'cascade'})
this.sequelize.sync({ force: true }).success(function() {
User.create({ username: 'foo' }).success(function(user) {
Task.create({ title: 'task' }).success(function(task) {
user.setTasks([task]).success(function() {
debugger
done()
})
})
})
})
})
})
}) })
}) })
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!