在 getManager.find 上找不到 typeorm 一对一实体

问题描述 投票:0回答:1

我有这个 typeorm 实体:

@Entity({ name: 'trans' })
export class Transaction {
  @PrimaryGeneratedColumn({ type: 'int', name: 'trans_id' })
  id: number;

  @OneToOne(type => Credit, credit => credit.transaction, { cascade: true })
  credit: Credit
}

有以下关系:

@Entity({ name: 'trans_credit' })
export class Credit {
  @PrimaryColumn({ type: 'int', name: 'trans_id' })
  id: number;
  
  @OneToOne(type => Transaction, transaction => transaction.credit)
  @JoinColumn({ name: 'trans_id' })
  transaction: Transaction;
}

trans_credit 表上没有定义 FK。对应的表是:

CREATE TABLE `trans` (`trans_id` int(10) unsigned NOT NULL AUTO_INCREMENT) AUTO_INCREMENT=1
CREATE TABLE `trans_credit` (`trans_id` int(10) unsigned NOT NULL)

这里提供的所有实体和表声明当然都是经过简化的。

当我尝试查询时:

const fromEntity = getRepository(Transaction).find({
            where: [
                { status: 'pending', credit: { j5: 1 } }
            ]
        });

我明白了:

Error: Relation with property path trans_id in entity was not found

为什么哦为什么会发生这种事?

node.js nestjs typeorm
1个回答
0
投票

您将

id
Credit
列名称指定为
trans_id
。并且您也将
JoinColumn
的名称定义为
trans_id

我认为

trans_id
在你的情况下应该是
credit_id

@Entity({ name: 'trans_credit' })
export class Credit {
  @PrimaryColumn({ type: 'int', name: 'credit_id' }) // <- updated the column name
  id: number;
  
  @OneToOne(type => Transaction, transaction => transaction.credit)
  @JoinColumn({ name: 'trans_id' })
  transaction: Transaction;
}
© www.soinside.com 2019 - 2024. All rights reserved.