Objection.js: Relations trouble

Created on 25 Oct 2016  路  1Comment  路  Source: Vincit/objection.js

Hello,

I'm having trouble with relations between tables,
I have a movies table like this:

Movies.relationMappings = {
    showings: {
        relation: Model.HasManyRelation,
        modelClass: __dirname + '/showings',
        join: {
            from: 'Movies.id',
            to: 'Showings.movieId'
        }
    }
};

and i have a showings table:

Showings.relationMappings = {
    movie: {
        relation: Model.BelongsToOneRelation,
        modelClass: __dirname + '/movies',
        join: {
            from: 'Showings.movieId',
            to: 'Movies.id'
        }
    }
};

I'm supposed to have a OneToManyRelation from Movies to Showings and a ManyToOneRelation from Showings to Movies.

I don't know if i'm doing something wrong with my relations but i can't insert datas to movies table using this Movies.query().insert({ title: 'Inception' }). I can't find anything in the docs that relates to this error, and i get Error {} using console.log.

Am I doing something wrong or did i just not understand a thing about how relations work with objectionjs?

Most helpful comment

Hello,

first thing, i got the error message using Movies.getRelations(), i had this error Movies.relationMappings.showings: join must be an object that maps the columns of the related models together. For example: {from: "SomeTable.id", to: "SomeOtherTable.someModelId"}.

i thought i had to use the model but it was actually the table name so it was:

Movies.relationMappings = {
    showings: {
        relation: Model.HasManyRelation,
        modelClass: __dirname + '/showings',
        join: {
            from: 'movies.id',      <-------- my table is lowercase
            to: 'showings.movieId'  <-------- same here
        }
    }
};

>All comments

Hello,

first thing, i got the error message using Movies.getRelations(), i had this error Movies.relationMappings.showings: join must be an object that maps the columns of the related models together. For example: {from: "SomeTable.id", to: "SomeOtherTable.someModelId"}.

i thought i had to use the model but it was actually the table name so it was:

Movies.relationMappings = {
    showings: {
        relation: Model.HasManyRelation,
        modelClass: __dirname + '/showings',
        join: {
            from: 'movies.id',      <-------- my table is lowercase
            to: 'showings.movieId'  <-------- same here
        }
    }
};
Was this page helpful?
0 / 5 - 0 ratings

Related issues

rickmed picture rickmed  路  4Comments

Gustav0ar picture Gustav0ar  路  4Comments

njleonzhang picture njleonzhang  路  4Comments

nazar picture nazar  路  3Comments

bsdo64 picture bsdo64  路  3Comments