Objection.js: How to handle has many through relationship?

Created on 15 Nov 2018  路  5Comments  路  Source: Vincit/objection.js

In Laravel there's a relation called Has Many Through and that is the description:

The "has-many-through" relationship provides a convenient shortcut for accessing distant relations via an intermediate relation. For example, a Country model might have many Post models through an intermediate User model. In this example, you could easily gather all blog posts for a given country. Let's look at the tables required to define this relationship:

countries
id - integer
name - string

users
id - integer
country_id - integer
name - string

posts
id - integer
user_id - integer
title - string

Though posts does not contain a country_id column, the hasManyThrough relation provides access to a country's posts via $country->posts. To perform this query, Eloquent inspects the country_id on the intermediate users table. After finding the matching user IDs, they are used to query the posts table.

The model would look like this:

class Country extends Model
{
    /**
     * Get all of the posts for the country.
     */
    public function posts()
    {
        return $this->hasManyThrough('App\Post', 'App\User');
    }
}

It was taken from https://laravel.com/docs/5.7/eloquent-relationships#has-many-through

What I want is something similar, how can I create this kind of relation with Objection.js?

Most helpful comment

So in this case:

class Country extends Model {
  static get relationMappings() {
    return {
      posts: {
        relation: Model.ManyToManyRelation,
        modelClass: Post,
        join: {
          from: 'countries.id',
          through: {
            from: 'users.country_id',
            to: 'users.id'
          },
          to: 'posts.user_id'
        }
      }
    }
  }
}

The syntax is aloooot more verbose because objection allows you to relate anything through any property without assuming any name conventions.

All 5 comments

Check out https://vincit.github.io/objection.js/#relationmappings

There is similar through param for a relation, most likely it is possible to do what you need with that.

I read but as far as I could understand the through is for many to many relationship when you have a pivot (join) table and not for distant relationships like has many through.

In objection you model all relationships that go through a table as ManyToManyRelationships.

So in this case:

class Country extends Model {
  static get relationMappings() {
    return {
      posts: {
        relation: Model.ManyToManyRelation,
        modelClass: Post,
        join: {
          from: 'countries.id',
          through: {
            from: 'users.country_id',
            to: 'users.id'
          },
          to: 'posts.user_id'
        }
      }
    }
  }
}

The syntax is aloooot more verbose because objection allows you to relate anything through any property without assuming any name conventions.

@koskimas That was amazing, thank you very much.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

chen7david picture chen7david  路  3Comments

haywirez picture haywirez  路  3Comments

bsdo64 picture bsdo64  路  3Comments

njleonzhang picture njleonzhang  路  4Comments

nicolaracco picture nicolaracco  路  3Comments