1

Is it possible in TypeORM, PostgreSQL, AdminJs with One to Many table relationships to immediately receive data from the associated table, and not a link to this data, as in my example.

@Entity()
export class User extends BaseEntity {
  @PrimaryGeneratedColumn()
  id: number;
 
  @Column({ unique: true })
  userColum: string;
 
  @OneToMany(() => Car, (car) => car.car)
  cars: Car[];
}

Car:----------------------------

@Entity()
export class Car extends BaseEntity {
  @PrimaryGeneratedColumn()
  id: number;
 
  @ManyToOne(() => User, (user: User) => user.userColum, { eager: true })
  car: Car;
 
  @Column()
  name: string;
}

ТI get a link here:

@ManyToOne(() => User, (user: User) => user.userColum, { eager: true })

But I would like to pull up the data itself. This seems to require an additional request. Or will you have to somehow custom make the request yourself? If yes, how can this be done?

Next I use AdminJS to display data and act on tables

I'm using AdminJS for the front-end (This requirement is not made by me. Therefore, I do not have routes, this is done by AdminJS under the hood. I did not find any mention of my situation in the AdminJS documentation. At the moment it turns out that instead of the desired data is the value from the userColum column, when I display the Cars table in a column called CarId, I get a reference to userColum. The link is displayed as an index of the Users table - 1, 2, 3 and is clickable. Here, instead of displaying 1, 2, 3, I want to see the text value “userColum 1”, “userColum 2”, “userColum 3”, which are in the Users column userColum. Accordingly, when I click on the link, I get the necessary data of the userColum column. And I want the data to be immediately displayed inside the Cars table.

If we remove { eager: true } Then in the database in the Cars table in the CarId column there will be id from the Users table, but nothing is displayed on the front-end AdminJs.

Illusion
  • 71
  • 9
  • How are you querying this data? Also, what exactly is the output here that you are getting vs desired? Are you getting just the `userColumn`? and you would like to get the whole `user` entity? is that correct? – Vin_it Jul 02 '22 at 20:33

1 Answers1

1

I believe you have a mistake in your Car entity (and a possible bad practice in User entity)

the mistake - it should be

@ManyToOne(() => User, (user: User) => user.cars, { eager: true })
user: User; // not car: Car;

and bad naming

@OneToMany(() => Car, (car) => car.user) // not car.car
cars: Car[];

Also, it would be helpful to know how you are querying this data. According to the docs

you must specify the relation in FindOptions

Like so:

const userRepository = dataSource.getRepository(User)
const users = await userRepository.find({
    relations: {
        cars: true,
    },
})
Vin_it
  • 186
  • 1
  • 5