You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Making simple associations is proving borderline impossible without modifying the (existing) db- / table-structure.
Versions
sequelize: 6.28.0
sequelize-typescript: 2.1.5
typescript: 4.7.4
Issue type
[x ] bug report
feature request
Actual behavior
I have two tables, Product and Supplier. In Product there is a column supplier_id which is a FK to supplier.id. However, whatever I am trying, this simple association does not work, giving me an Error 'Error: Foreign key for "Product" is missing on "Supplier".'.
Expected behavior
Not give me an error and create a simple foreign key Product.supplier_id referencing Supplier.id.
Steps to reproduce
Creating those model classes and trying to sync them.
It really doesn't matter how and in which order I decorate the respective fields, using @BelongsTo, @HasMany, @HasOne, @ForeignKey, it always spits out the same error Error: Foreign key for "Product" is missing on "Supplier"..
Solution
As it always is, once you create an issue / open a thread about a problem, you find the solution yourself. In this case I had an oversight in reading the docs and threads on SO and in the issues.
In order to declare associations per Foreign Key, the target Model needs a property of the source Model type, but not as a column.
In my case it was
@HasMany(()=>Product)products!: Product[]
The text was updated successfully, but these errors were encountered:
Issue
Making simple associations is proving borderline impossible without modifying the (existing) db- / table-structure.
Versions
Issue type
Actual behavior
I have two tables,
Product
andSupplier
. InProduct
there is a columnsupplier_id
which is a FK tosupplier
.id
. However, whatever I am trying, this simple association does not work, giving me an Error 'Error: Foreign key for "Product" is missing on "Supplier".'.Expected behavior
Not give me an error and create a simple foreign key
Product
.supplier_id
referencingSupplier
.id
.Steps to reproduce
Creating those model classes and trying to sync them.
Related code
Product Model
Supplier Model
It really doesn't matter how and in which order I decorate the respective fields, using
@BelongsTo
,@HasMany
,@HasOne
,@ForeignKey
, it always spits out the same errorError: Foreign key for "Product" is missing on "Supplier".
.Solution
As it always is, once you create an issue / open a thread about a problem, you find the solution yourself. In this case I had an oversight in reading the docs and threads on SO and in the issues.
In order to declare associations per Foreign Key, the target Model needs a
property
of the source Model type, but not as a column.In my case it was
The text was updated successfully, but these errors were encountered: