typeorm one-on-one entity not found on getManager.find

Viewed 4138

I have this typeorm entity:

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

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

With the following relation:

@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;
}

There is no FK defined on the trans_credit table. Corresponding tables are:

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)

All entities and table declarations presented here are simplified of course.

When I try to query:

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

I get this:

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

Why oh why is this happening?

1 Answers

You specified your id column name of Credit as trans_id. And you defined the name of JoinColumn as trans_id too.

I think trans_id should be credit_id in your case:

@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;
}
Related