一对一关系不适用于 TypeORM

One to One relation is not working on TypeORM

我有两个模型,它们是一对一关系的参考。

@Entity()
class Restaurant {

    @PrimaryGeneratedColumn()
    id: number;

    @Column({
        type: 'varchar',
        length: 1000,
        unique: true
    })
    name: string;

    @Column()
    @OneToOne(() => RestaurantType)
    @JoinColumn()
    restaurantType: RestaurantType;
}
@Entity()
class RestaurantType {

    @PrimaryGeneratedColumn()
    id: number;

    @Column({nullable: false})
    resturantType: string;

    @Column({
        type: 'time with time zone', 
        default: Date.now()
    })
    createdAt: Date;
}

但是我在 运行 服务器

时看到这个错误
DataTypeNotSupportedError: Data type "RestaurantType" in "Restaurant.restaurantType" is not supported by "postgres" database.

为什么会这样?

您应该从 Restaurant 实体的 restaurantType 属性 中移除 @Column() 装饰器。

@Entity()
class Restaurant {

    @PrimaryGeneratedColumn()
    id: number;

    @Column({
        type: 'varchar',
        length: 1000,
        unique: true
    })
    name: string;

    @OneToOne(() => RestaurantType)
    @JoinColumn()
    restaurantType: RestaurantType;
}

有关 OneToOne 关系的更多信息,请参阅 docs