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

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