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

Commit d8cfd984 by Meg Sharkey

allow association table name to be specified, pass through freeze table name and underscore options

1 parent 1bf57680
...@@ -10,7 +10,7 @@ var HasMany = module.exports = function(srcModel, targetModel, options) { ...@@ -10,7 +10,7 @@ var HasMany = module.exports = function(srcModel, targetModel, options) {
this.options = options this.options = options
this.isSelfAssociation = (this.source.tableName == this.target.tableName) this.isSelfAssociation = (this.source.tableName == this.target.tableName)
this.associationAccessor = this.combinedName = Utils.combineTableNames( this.associationAccessor = this.combinedName = this.options.joinTableName || Utils.combineTableNames(
this.source.tableName, this.source.tableName,
this.isSelfAssociation ? (this.options.as || this.target.tableName) : this.target.tableName this.isSelfAssociation ? (this.options.as || this.target.tableName) : this.target.tableName
) )
...@@ -36,7 +36,7 @@ HasMany.prototype.injectAttributes = function() { ...@@ -36,7 +36,7 @@ HasMany.prototype.injectAttributes = function() {
if (this.isSelfAssociation || multiAssociation) { if (this.isSelfAssociation || multiAssociation) {
// remove the obsolete association identifier from the source // remove the obsolete association identifier from the source
if(this.isSelfAssociation) { if(this.isSelfAssociation) {
this.foreignIdentifier = (this.options.as || this.target.tableName) + 'Id' this.foreignIdentifier = Utils._.underscoredIf((this.options.as || this.target.tableName) + 'Id', this.options.underscored)
} else { } else {
this.foreignIdentifier = this.target.associations[this.associationAccessor].identifier this.foreignIdentifier = this.target.associations[this.associationAccessor].identifier
delete this.source.attributes[this.foreignIdentifier] delete this.source.attributes[this.foreignIdentifier]
...@@ -47,7 +47,7 @@ HasMany.prototype.injectAttributes = function() { ...@@ -47,7 +47,7 @@ HasMany.prototype.injectAttributes = function() {
combinedTableAttributes[this.identifier] = {type:DataTypes.INTEGER, primaryKey: true} combinedTableAttributes[this.identifier] = {type:DataTypes.INTEGER, primaryKey: true}
combinedTableAttributes[this.foreignIdentifier] = {type:DataTypes.INTEGER, primaryKey: true} combinedTableAttributes[this.foreignIdentifier] = {type:DataTypes.INTEGER, primaryKey: true}
this.connectorModel = this.source.modelManager.sequelize.define(this.combinedName, combinedTableAttributes) this.connectorModel = this.source.modelManager.sequelize.define(this.combinedName, combinedTableAttributes, this.options)
if(!this.isSelfAssociation) this.target.associations[this.associationAccessor].connectorModel = this.connectorModel if(!this.isSelfAssociation) this.target.associations[this.associationAccessor].connectorModel = this.connectorModel
this.connectorModel.sync() this.connectorModel.sync()
......
...@@ -21,6 +21,7 @@ describe('Associations', function() { ...@@ -21,6 +21,7 @@ describe('Associations', function() {
expect(sequelize.modelManager.getModel('wp_table1swp_table2s')).toBeDefined() expect(sequelize.modelManager.getModel('wp_table1swp_table2s')).toBeDefined()
}) })
}) })
// TODO add test for specifying join table name
}) })
}) })
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!