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

Commit b6c2e421 by Sushant Committed by GitHub

fix(hasOne.sourceKey): setup sourceKeyAttribute for joins (#9658)

1 parent c90c8d5c
...@@ -80,7 +80,6 @@ class HasMany extends Association { ...@@ -80,7 +80,6 @@ class HasMany extends Association {
* Source key setup * Source key setup
*/ */
this.sourceKey = this.options.sourceKey || this.source.primaryKeyAttribute; this.sourceKey = this.options.sourceKey || this.source.primaryKeyAttribute;
this.sourceIdentifier = this.sourceKey;
if (this.source.rawAttributes[this.sourceKey]) { if (this.source.rawAttributes[this.sourceKey]) {
this.sourceKeyAttribute = this.sourceKey; this.sourceKeyAttribute = this.sourceKey;
......
...@@ -55,10 +55,9 @@ class HasOne extends Association { ...@@ -55,10 +55,9 @@ class HasOne extends Association {
throw new Error(`Unknown attribute "${this.options.sourceKey}" passed as sourceKey, define this attribute on model "${this.source.name}" first`); throw new Error(`Unknown attribute "${this.options.sourceKey}" passed as sourceKey, define this attribute on model "${this.source.name}" first`);
} }
this.sourceKey = this.options.sourceKey || this.source.primaryKeyAttribute; this.sourceKey = this.sourceKeyAttribute = this.options.sourceKey || this.source.primaryKeyAttribute;
this.sourceKeyField = this.source.rawAttributes[this.sourceKey].field || this.sourceKey; this.sourceKeyField = this.source.rawAttributes[this.sourceKey].field || this.sourceKey;
this.sourceKeyIsPrimary = this.sourceKey === this.source.primaryKeyAttribute; this.sourceKeyIsPrimary = this.sourceKey === this.source.primaryKeyAttribute;
this.sourceIdentifier = this.sourceKey;
this.associationAccessor = this.as; this.associationAccessor = this.as;
this.options.useHooks = options.useHooks; this.options.useHooks = options.useHooks;
...@@ -218,7 +217,7 @@ class HasOne extends Association { ...@@ -218,7 +217,7 @@ class HasOne extends Association {
} }
_.assign(associatedInstance, this.scope); _.assign(associatedInstance, this.scope);
associatedInstance.set(this.foreignKey, sourceInstance.get(this.sourceIdentifier)); associatedInstance.set(this.foreignKey, sourceInstance.get(this.sourceKeyAttribute));
return associatedInstance.save(options); return associatedInstance.save(options);
} }
...@@ -250,7 +249,7 @@ class HasOne extends Association { ...@@ -250,7 +249,7 @@ class HasOne extends Association {
} }
} }
values[this.foreignKey] = sourceInstance.get(this.sourceIdentifier); values[this.foreignKey] = sourceInstance.get(this.sourceKeyAttribute);
if (options.fields) { if (options.fields) {
options.fields.push(this.foreignKey); options.fields.push(this.foreignKey);
} }
......
...@@ -65,7 +65,7 @@ ...@@ -65,7 +65,7 @@
"semantic-release": "^15.x", "semantic-release": "^15.x",
"sinon": "^6.0.0", "sinon": "^6.0.0",
"sinon-chai": "^3.2.0", "sinon-chai": "^3.2.0",
"sqlite3": "^4.x", "sqlite3": "^4.0.1",
"tedious": "^2.x", "tedious": "^2.x",
"uuid-validate": "^0.0.3", "uuid-validate": "^0.0.3",
"validate-commit-msg": "^2.12.2" "validate-commit-msg": "^2.12.2"
......
...@@ -668,8 +668,8 @@ describe(Support.getTestDialectTeaser('HasOne'), () => { ...@@ -668,8 +668,8 @@ describe(Support.getTestDialectTeaser('HasOne'), () => {
it('should support a non-primary key as the association column on a target with custom primary key', function() { it('should support a non-primary key as the association column on a target with custom primary key', function() {
const User = this.sequelize.define('User', { const User = this.sequelize.define('User', {
user_name: { user_name: {
type: Sequelize.STRING, unique: true,
primaryKey: true type: Sequelize.STRING
} }
}); });
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!