Search code examples
many-to-manyentitytypeorm

Can I use additional fields in TypeOrm many-to-many join table


I have two tables: Painting and Exhibit with tables painting and exhibit whose many-to-many relationships are handled by the exhibit_paintings table - id, paintingId, exhibitId.

Because paintings can have a price unique to an exhibit, I want to use an extra field - exhibitpaintingprice - in the exhibit_painting table. How do I access that field on a nextjs page?

How should I change this otherwise successful code?

These two lines will produce a collection of paintings, without the exhibitpaintingprice.

  const exhibitRepository = AppDataSource.getRepository(Exhibit);
  const exhibit = await exhibitRepository.findOne({
    where: { id: exhibitid },
    relations: ["gallery", "paintings"],
  });
  return exhibit;
} 

table body that mapped the collection :

                  <>
                    {exhibit.paintings ? (
                      exhibit.paintings.map((painting) => {
                        const exhibitpainting = painting.exhibitRelations;

                        return (
                          <tr key={painting.id}>
                            <td>{painting.id}</td>
                            <td>
                              <Image
                                src={painting.picture}
                                alt={painting.title}
                                width={100}
                                height={100}
                              />
                            </td>
                            <td>
                              <Link href={`/painting?id=${painting.id}`}>
                                {painting.title}
                              </Link>
                            </td>
                            <td>{painting.description}</td>
                            <td>Exhibit note</td>
                            <td>{painting.media}</td>
                            <td>
                              {painting.height}h x {painting.width}w x .5d
                            </td>
                            <td>{painting.price}</td>
**<td>ExhibitPaintingPrice: {??????????????}</td>**
                          </tr>
                        );
                      })
                    ) : (
                      <tr>
                        <td colSpan={7}>No paintings</td>
                      </tr>
                    )}
                  </>

I tried

                            <td>
                              {painting.exhibitRelations?.exhibitpaintingprice}
                            </td>

generated this error: - error TypeError: Cannot read properties of undefined (reading 'joinColumns')


Solution

  • You will have to create a separate table by yourself instead of relying on join table provided by TypeORM.

    Create another entity ExhibitPaintings and add two primary columns for each id. And then add ManyToOne decorators to each table and specify the join columns as those primary columns that you just created. And then your many-to-many table is ready. Now, you can add any custom column to this table, and can query with either QuerySelector or simply via EntityManager. Here is the GitHub discussion that would you help you. https://github.com/typeorm/typeorm/issues/2402