【问题标题】:One to One relation is not working on TypeORM一对一关系不适用于 TypeORM
【发布时间】:2021-11-22 17:55:28
【问题描述】:

我有两个模型参考一对一关系。

@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.

为什么会这样?

【问题讨论】:

    标签: javascript typescript orm typeorm


    【解决方案1】:

    您应该从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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-12-31
      • 2018-02-03
      • 2023-03-17
      • 1970-01-01
      • 2021-01-04
      • 1970-01-01
      • 2018-07-30
      相关资源
      最近更新 更多