I have two model AdminUser, Organization, and I want to use unique key Organzation.code assoction AdminUser.organizationCode instead of primary key id
export class AdminUser extends Model<AdminUser> {
@PrimaryKey
@AutoIncrement
@Column
public id: number;
@ForeignKey(() => Organization)
public organizationCode: string;
@BelongsTo(() => Organization, "organizationCode")
public organization: Organization;
}
export class Organization extends Model<Organization> {
@PrimaryKey
@AutoIncrement
@Column
public id: number;
@Unique
@Column
public code: string;
@HasOne(() => AdminUser)
public adminUser: AdminUser;
}
but sql query is still use organization.id associate

how to make right associate like AdminUser`.`organizationCode` = `organization`.`code
Hey @chaoxihailing, actually you need to use targetKey like so:
@BelongsTo(() => Organization, {targetKey: 'code'})
鈿狅笍But this doesn't seem to work with HasOne. Regarding this it is an issue with sequelize.
So you could use @HasMany as a workaround :/
thanks锛宨t can work normally
Most helpful comment
Hey @chaoxihailing, actually you need to use
targetKeylike so:鈿狅笍But this doesn't seem to work with
HasOne. Regarding this it is an issue with sequelize.So you could use
@HasManyas a workaround :/