簡體   English   中英

一對一關系不適用於 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關系的更多信息,請參閱文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM