New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Association with same Model returns dup object #1692
Comments
Worked by adding the foreignKey right after the Model @Column
@ForeignKey(() => Profile)
ContractorId!: string
@BelongsTo(() => Profile, 'ContractorId')
Contractor!: Profile
@Column
@ForeignKey(() => Profile)
ClientId!: string
@BelongsTo(() => Profile, 'ClientId')
Client!: Profile Debugging I found that on class Association {
constructor(source, target, options = {}) {
this.source = source;
this.target = target;
this.options = options;
this.scope = options.scope;
this.isSelfAssociation = this.source === this.target;
this.as = options.as;
this.associationType = "";
console.log(12, 'Association Class =>', this.source.name, options.as, options.foreignKey)
if (source.hasAlias(options.as)) {
throw new AssociationError(`You have used the alias ${options.as} in two separate associations. Aliased associations must have unique aliases.`);
}
}
....
}
// Log Output: 12 Association Class => Contract Contractor { name: 'ContractorId' }
// Log Output: 12 Association Class => Contract Client { name: 'ContractorId' } |
Update: File: foreign-key-service.ts#L34 The [
{
relatedClassGetter: [Function (anonymous)],
foreignKey: 'ContractorId'
},
{
relatedClassGetter: [Function (anonymous)],
foreignKey: 'ClientId'
}
] function getForeignKeyOptions(relatedClass, classWithForeignKey, foreignKey) {
let foreignKeyOptions = {};
...
if (!foreignKeyOptions.name && classWithForeignKey) {
console.log(0, classWithForeignKey)
const foreignKeys = getForeignKeys(classWithForeignKey.prototype) || [];
for (let key of foreignKeys) {
if (key.relatedClassGetter() === relatedClass ||
relatedClass.prototype instanceof key.relatedClassGetter()) {
foreignKeyOptions.name = key.foreignKey;
break;
}
}
}
...
return foreignKeyOptions;
}
exports.getForeignKeyOptions = getForeignKeyOptions; |
Issue
Versions
Issue type
Actual behavior
Expected behavior
Respective data being retrieved from each association
Steps to reproduce
NOTE: Also tested with some other combinations, like only calling the class/model or with the following:
Related code
The issue is occurring at the LEFT OUTER JOIN.
Both compare using
ON Contract.ContractorId
The expected query should be to compare
CLIENT
usingON Contract.ClientId
andCONTRACTOR
usingON Contract.ContractorId
NOTE: The other clauses like SELECT and WHERE were omitted since they are perfect
Table: Contract
Table: Profile
The text was updated successfully, but these errors were encountered: