1
votes

TypeORM is making a weird query of clientId. I have a store and a client table. A client can have many stores. A store can have a client.

I have tried using the ManyToOne and OneToMany relationship.

Store:

@ManyToOne(type => ClientRelationalEntity, client => client.stores)
client: ClientRelationalEntity;

Client:

@OneToMany(type => StoreRelationalEntity, store => store.client)
stores: StoreRelationalEntity[];

I get a query error:

ER_BAD_FIELD_ERROR: Unknown column 'StoreRelationalEntity.clientId' in 'field list'

'id' is being added to client. If I change the id variable in client.js to 'fgfgfg' the error is:

ER_BAD_FIELD_ERROR: Unknown column 'StoreRelationalEntity.clientfgfgfg' in 'field list'

What am I doing wrong here?

1
Can you post your database schema for those two tables? You can omit all of the columns minus the ids / foreign keys if you want.Dylan Aspden
Client has an ID primary key column. Store has a client_id column which is a foreign key linked to the client table ID.jackabe
In your store entity maybe try adding @JoinColumn({ name: 'client_id' }) decorator to the client field.Dylan Aspden
This is happening to me too. Did you find any solution to this?Harshit Bhatt

1 Answers

0
votes

Try following code this may help you.

// Store
@ManyToOne(type => ClientRelationalEntity, client => client.id)
client: ClientRelationalEntity;

// Client:
@OneToMany(type => StoreRelationalEntity, store => store.client)
stores: StoreRelationalEntity[];