Sequelize-typescript: Multiple relations to same model

Created on 31 Dec 2017  路  2Comments  路  Source: RobinBuschmann/sequelize-typescript

I have this Product model with multiple relations to the Lookup model:

```
// Product model
@BelongsTo(() => Lookup, 'LkpCountyOid')
county: Lookup;

@BelongsTo(() => Lookup, 'LkpStateOid')
state: Lookup

@Column
LkpCountyOid: number;

@Column
LkpStateOid: number;

```

Example query:

const productData = await Product.find({ include: [ { model: Lookup, as: "LkpStateOid"} ] });

My understanding from the docs and this issue is that the "as" property should allow for multiple relations.

Here is the error I'm getting:

SequelizeEagerLoadingError: Lookup is associated to Product multiple times. To identify the correct association, you must use the 'as' keyword to specify the alias of the association you want to include.

Thanks in advance for your help!

Most helpful comment

Hey @dlemburg you need to use the property name of the related model instead of the foreign key for as. { model: Lookup, as: "state"}.

All 2 comments

Hey @dlemburg you need to use the property name of the related model instead of the foreign key for as. { model: Lookup, as: "state"}.

Ah, excellent. Thank you for the quick response and great library!

Was this page helpful?
0 / 5 - 0 ratings