orchid-orm icon indicating copy to clipboard operation
orchid-orm copied to clipboard

[ask] Add arbitrary `ON` conditions for `relations`

Open mordechaim opened this issue 1 year ago • 2 comments

I'm trying to create a relation definition for a model, but it's not just a map between a reference ID, but I want to add more conditions to the ON clause. Is it possible to define more conditions for relations?

Example:

SELECT a.name, b.value 
FROM table_a a
JOIN table_b b ON a.b_id = b.id AND b.type = 'primary'

How do I add b.type === 'primary' without going down the rabbit hole of building my own query?

mordechaim avatar Jul 17 '24 18:07 mordechaim

Currently this isn't supported, but it's quite essential feature and I'll try to add it soon.

To have:

relations = {
  things: this.hasMany(() => Thing, {
    columns: ['id'],
    references: ['thingId'],
    where: {
      type: 'primary',
    },
  })
}

And it should not just be added to ON, but also added when creating a sub-record.

romeerez avatar Jul 26 '24 23:07 romeerez

Thanks, for now I figured I can add conditions in the query itself, on the relational select statement.

mordechaim avatar Jul 26 '24 23:07 mordechaim

Ready! the new on option

romeerez avatar Feb 02 '25 14:02 romeerez

This is great 👍 thanks @romeerez

mordechaim avatar Feb 02 '25 14:02 mordechaim