romeerez/orchid-orm

[ask] use `joinLeft(xxx)` multiple times

Closed this issue · 4 comments

I write a queryMethod in repo, which needs leftJoin('user'), this method may be called more than once in a query, when called multiple times, it shows an error of table name "user" specified more than once,

how to safely call leftJoin('user') more than once.

It depends, hard to suggest without seeing the actual code.

I guess I could make a change so that if you join the same table in the same way more than once, then it ignores all joins except the first.

I'll make a reproduction repo later

Ready! Handed in orchid-orm v1.23.3.

Thank you for the repro! Helps a lot.

Changed repo query method to have just leftJoin instead of conditional, as now this is supported.

import { createRepo, raw } from 'orchid-orm';
import { db } from '../tables';

export const activityMember = createRepo(db.activityMember, {
  queryMethods: {
    whereByUserVipTo: (q, operator: '>' | '<' | '>=' | '<=' | '=' | '!=', date: Date) => {
      return q.leftJoin('user').where(
        raw({ raw: `("user"."vip"->>'to')::timestamp ${operator} '${date.toISOString()}'::timestamp` }),
      );
    },
  },
});

Duplicated joins are now ignored automatically. Updated join docs.