Getting related model this.belongsTo is not a function


#1

Hi all,

So I have some unexpected troubles with relations:

I have a ShippingOrder with an loadingAddress and unloadingAddress as foreign relation.
e.g.:
table.string('loadingAddress').references('name').inTable('addresses').notNullable(); table.string('unloadingAddress').references('name').inTable('addresses').notNullable();
And:

  this.create('addresses', (table) => {
      table.string('name').primary();
      table.string('street');
      table.string('instruction');
      table.string('zipCode');
      table.string('city');
      table.string('country');
      table.string('businessHours');
      table.string('contactPerson');
      table.string('phone');
      table.string('email');
      table.timestamps()
    })

if i try to get the loading address with

            .query()
            .where('id', id)
            .with('loadingAddress')
            .fetch();

and in my ShippingOrder model:
loadingAddress () { return this.belongsTo('App/Models/Address', 'loadingAddress', 'name'); }

it will always return this.belongsTo is not a function anyone got an idea?

this is the stacktrace:

Could it be the same problem as described here? @virk


#2

Ok, so I got it working by renaming my method???
If someone still has an explanation, I’d love to hear it!