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

Commit 1df90277 by Martin Aspeli

Don't pretend like composite primary keys work: they need to be handled at table…

… level, not attribute level
1 parent 03ca0291
...@@ -26,10 +26,14 @@ module.exports = (function() { ...@@ -26,10 +26,14 @@ module.exports = (function() {
newAttributes[this.identifier] = { type: DataTypes.INTEGER } newAttributes[this.identifier] = { type: DataTypes.INTEGER }
if(this.options.foreignKeyConstraint || this.options.onDelete || this.options.onUpdate) { if(this.options.foreignKeyConstraint || this.options.onDelete || this.options.onUpdate) {
newAttributes[this.identifier].references = this.target.tableName, var primaryKeys = Utils._.filter(Utils._.keys(this.target.rawAttributes), function(key) { return this.target.rawAttributes[key].primaryKey }, this)
newAttributes[this.identifier].referencesKeys = Utils._.filter(Utils._.keys(this.target.rawAttributes), function(key) { return this.target.rawAttributes[key].primaryKey }, this)
newAttributes[this.identifier].onDelete = this.options.onDelete, if(primaryKeys.length == 1) { // composite keys not supported with this approach
newAttributes[this.identifier].onUpdate = this.options.onUpdate newAttributes[this.identifier].references = this.target.tableName,
newAttributes[this.identifier].referencesKey = primaryKeys[0]
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)
......
...@@ -67,10 +67,14 @@ module.exports = (function() { ...@@ -67,10 +67,14 @@ module.exports = (function() {
newAttributes[this.identifier] = { type: DataTypes.INTEGER } newAttributes[this.identifier] = { type: DataTypes.INTEGER }
if(this.options.foreignKeyConstraint || this.options.onDelete || this.options.onUpdate) { if(this.options.foreignKeyConstraint || this.options.onDelete || this.options.onUpdate) {
newAttributes[this.identifier].references = this.source.tableName var primaryKeys = Utils._.filter(Utils._.keys(this.source.rawAttributes), function(key) { return this.source.rawAttributes[key].primaryKey }, this)
newAttributes[this.identifier].referencesKeys = Utils._.filter(Utils._.keys(this.source.rawAttributes), function(key) { return this.source.rawAttributes[key].primaryKey }, this)
newAttributes[this.identifier].onDelete = this.options.onDelete if(primaryKeys.length == 1) { // composite keys not supported with this approach
newAttributes[this.identifier].onUpdate = this.options.onUpdate newAttributes[this.identifier].references = this.source.tableName
newAttributes[this.identifier].referencesKey = primaryKeys[0]
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)
......
...@@ -31,10 +31,14 @@ module.exports = (function() { ...@@ -31,10 +31,14 @@ module.exports = (function() {
newAttributes[this.identifier] = { type: DataTypes.INTEGER } newAttributes[this.identifier] = { type: DataTypes.INTEGER }
if(this.options.foreignKeyConstraint || this.options.onDelete || this.options.onUpdate) { if(this.options.foreignKeyConstraint || this.options.onDelete || this.options.onUpdate) {
newAttributes[this.identifier].references = this.source.tableName, var primaryKeys = Utils._.filter(Utils._.keys(this.source.rawAttributes), function(key) { return this.source.rawAttributes[key].primaryKey }, this)
newAttributes[this.identifier].referencesKeys = Utils._.filter(Utils._.keys(this.source.rawAttributes), function(key) { return this.source.rawAttributes[key].primaryKey }, this)
newAttributes[this.identifier].onDelete = this.options.onDelete, if(primaryKeys.length == 1) { // composite keys not supported with this approach
newAttributes[this.identifier].onUpdate = this.options.onUpdate newAttributes[this.identifier].references = this.source.tableName,
newAttributes[this.identifier].referencesKey = primaryKeys[0]
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)
......
...@@ -454,9 +454,8 @@ module.exports = (function() { ...@@ -454,9 +454,8 @@ module.exports = (function() {
template += " REFERENCES " + Utils.addTicks(dataType.references) template += " REFERENCES " + Utils.addTicks(dataType.references)
if(dataType.referencesKeys) { if(dataType.referencesKey) {
// TODO: This isn't really right - for composite primary keys we need a different approach template += " (" + Utils.addTicks(dataType.referencesKey) + ")"
template += " (" + dataType.referencesKeys.map(Utils.addTicks).join(', ') + ")"
} else { } else {
template += " (" + Utils.addTicks('id') + ")" template += " (" + Utils.addTicks('id') + ")"
} }
......
...@@ -557,14 +557,13 @@ module.exports = (function() { ...@@ -557,14 +557,13 @@ module.exports = (function() {
} }
if(dataType.references) { if(dataType.references) {
template += " REFERENCES <%= referencesTable %> (<%= referencesKeys %>)" template += " REFERENCES <%= referencesTable %> (<%= referencesKey %>)"
replacements.referencesTable = QueryGenerator.addQuotes(dataType.references) replacements.referencesTable = QueryGenerator.addQuotes(dataType.references)
if(dataType.referencesKeys) { if(dataType.referencesKey) {
// TODO: This isn't really right - for composite primary keys we need a different approach replacements.referencesKey = QueryGenerator.addQuotes(dataType.referencesKey)
replacements.referencesKeys = dataType.referencesKeys.map(QueryGenerator.addQuotes).join(', ')
} else { } else {
replacements.referencesKeys = QueryGenerator.addQuotes('id') replacements.referencesKey = QueryGenerator.addQuotes('id')
} }
if(dataType.onDelete) { if(dataType.onDelete) {
......
...@@ -218,14 +218,13 @@ module.exports = (function() { ...@@ -218,14 +218,13 @@ module.exports = (function() {
} }
if(dataType.references) { if(dataType.references) {
template += " REFERENCES <%= referencesTable %> (<%= referencesKeys %>)" template += " REFERENCES <%= referencesTable %> (<%= referencesKey %>)"
replacements.referencesTable = Utils.addTicks(dataType.references) replacements.referencesTable = Utils.addTicks(dataType.references)
if(dataType.referencesKeys) { if(dataType.referencesKey) {
// TODO: This isn't really right - for composite primary keys we need a different approach replacements.referencesKey = Utils.addTicks(dataType.referencesKey)
replacements.referencesKeys = dataType.referencesKeys.map(Utils.addTicks).join(', ')
} else { } else {
replacements.referencesKeys = Utils.addTicks('id') replacements.referencesKey = Utils.addTicks('id')
} }
if(dataType.onDelete) { if(dataType.onDelete) {
......
...@@ -49,7 +49,7 @@ describe('QueryGenerator', function() { ...@@ -49,7 +49,7 @@ describe('QueryGenerator', function() {
expectation: {id: 'INTEGER REFERENCES `Bar` (`id`)'} expectation: {id: 'INTEGER REFERENCES `Bar` (`id`)'}
}, },
{ {
arguments: [{id: {type: 'INTEGER', references: 'Bar', referencesKeys: ['pk']}}], arguments: [{id: {type: 'INTEGER', references: 'Bar', referencesKey: 'pk'}}],
expectation: {id: 'INTEGER REFERENCES `Bar` (`pk`)'} expectation: {id: 'INTEGER REFERENCES `Bar` (`pk`)'}
}, },
{ {
......
...@@ -53,7 +53,7 @@ describe('QueryGenerator', function() { ...@@ -53,7 +53,7 @@ describe('QueryGenerator', function() {
expectation: {id: 'INTEGER REFERENCES "Bar" ("id")'} expectation: {id: 'INTEGER REFERENCES "Bar" ("id")'}
}, },
{ {
arguments: [{id: {type: 'INTEGER', references: 'Bar', referencesKeys: ['pk']}}], arguments: [{id: {type: 'INTEGER', references: 'Bar', referencesKey: 'pk'}}],
expectation: {id: 'INTEGER REFERENCES "Bar" ("pk")'} expectation: {id: 'INTEGER REFERENCES "Bar" ("pk")'}
}, },
{ {
......
...@@ -48,7 +48,7 @@ describe('QueryGenerator', function() { ...@@ -48,7 +48,7 @@ describe('QueryGenerator', function() {
expectation: {id: 'INTEGER REFERENCES `Bar` (`id`)'} expectation: {id: 'INTEGER REFERENCES `Bar` (`id`)'}
}, },
{ {
arguments: [{id: {type: 'INTEGER', references: 'Bar', referencesKeys: ['pk']}}], arguments: [{id: {type: 'INTEGER', references: 'Bar', referencesKey: 'pk'}}],
expectation: {id: 'INTEGER REFERENCES `Bar` (`pk`)'} expectation: {id: 'INTEGER REFERENCES `Bar` (`pk`)'}
}, },
{ {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!