Sequelize-typescript: Error: Foreign key for "User" is missing on "Person" [Repo included]

Created on 23 Mar 2020  路  1Comment  路  Source: RobinBuschmann/sequelize-typescript

Hi, possibly related to #751

Versions

  • sequelize: 5.21.5
  • sequelize-typescript: 1.1.0
  • typescript: 3.8.3

I'm submitting a ...

[x] bug report
[ ] feature request

Having an issue with foreign keys. Here are my models:

person.ts

import { Table, BelongsTo, PrimaryKey, Column, Length, Model } from "sequelize-typescript";
import User from './user'

@Table
export default class Person extends Model<Person> {

    @Length({min: 2, max: 24})
    @Column
    name: string

    @BelongsTo(() => User)
    user: User
}

user.ts

import {Table, Column, Model, HasMany, PrimaryKey, IsEmail, Length, Default} from 'sequelize-typescript';
import Person from './person';

@Table
export default class User extends Model<User> {

    @IsEmail
    @Column
    email: string

    @HasMany(() => Person)
    persons: Person[]
}

When I create a Sequelize instance as described in the docs with the models, I get

        throw new Error(`Foreign key for "${relatedClass.name}" is missing ` +
              ^
Error: Foreign key for "User" is missing on "Person".

Not really sure why. I tried to follow the docs as closely as I could.

Reproducing

Clone my example repo. Run npm i and then npm run start.

Most helpful comment

Forgot

    @ForeignKey(() => User)
    userId: number

In Person model. Oops.

>All comments

Forgot

    @ForeignKey(() => User)
    userId: number

In Person model. Oops.

Was this page helpful?
0 / 5 - 0 ratings