adonisjs / core

AdonisJS is a TypeScript-first web framework for building web apps and API servers. It comes with support for testing, modern tooling, an ecosystem of official packages, and more.
https://adonisjs.com
MIT License
16.9k stars 639 forks source link

make simple relation ship and get error #355

Closed pishguy closed 7 years ago

pishguy commented 7 years ago

I cant make simple relation ship between two table

Screen Shot

my AlachiqChannel model

channelsTypeId() {
    return this.belongsTo('App/Model/ChannelsType')
}

and ChannelsType model

alachigChannels() {
    return this.hasMany('App/Model/AlachiqChannel')
}

const channel = yield AlachiqChannel.find(1);
const channelType = yield channel.channelsTypeId().fetch();

I get this error:

Trying to fetch relationship with channels_type_id as primaryKey, whose value is falsy
thetutlage commented 7 years ago

Can u share the value of channel.channels_type_id

pishguy commented 7 years ago

@thetutlage yes sure, i get undefined unfortunately

for more information:

AlachiqChannels: Screen Shot

and that model:

'use strict'

const Lucid = use('Lucid')

class AlachiqChannel extends Lucid {
    static get table() {
        return 'alachiqChannels'
    }

    channelsTypeId() {
        return this.belongsTo('App/Model/ChannelsType')
    }
}

module.exports = AlachiqChannel

ChannelsType: Screen Shot

and that model:

'use strict'

const Lucid = use('Lucid')

class ChannelsType extends Lucid {
    static get table() {
        return 'channelsType'
    }

    alachigChannels() {
        return this.hasMany('App/Model/AlachiqChannel')
    }

}

module.exports = ChannelsType
thetutlage commented 7 years ago

What is the value of channels_type_id with in the database?

pishguy commented 7 years ago

in alachiqChannels table i have more row which all of them have 1 on channels_type_id and they are referenced on id on channelsType and on channelsType that have one row with 1 data

alachiqChannels: Screen Shot

channelTypes: Screen Shot

Full Stack error:

WARN adonis:lucid +5ms Trying to fetch relationship with channels_type_id as primaryKey, whose value is falsy
Error: Undefined binding(s) detected when compiling SELECT query: select * from `channelsType` where `id` = ? limit ?
    at QueryCompiler_MySQL.toSQL (/Applications/XAMPP/xamppfiles/htdocs/alachiq/node_modules/knex/lib/query/compiler.js:78:13)
    at QueryBuilder.toSQL (/Applications/XAMPP/xamppfiles/htdocs/alachiq/node_modules/knex/lib/query/builder.js:61:44)
    at /Applications/XAMPP/xamppfiles/htdocs/alachiq/node_modules/knex/lib/runner.js:46:32
From previous event:
    at Runner.run (/Applications/XAMPP/xamppfiles/htdocs/alachiq/node_modules/knex/lib/runner.js:41:33)
    at QueryBuilder.Target.then (/Applications/XAMPP/xamppfiles/htdocs/alachiq/node_modules/knex/lib/interface.js:32:43)
    at next (/Applications/XAMPP/xamppfiles/htdocs/alachiq/node_modules/co/index.js:100:51)
    at onFulfilled (/Applications/XAMPP/xamppfiles/htdocs/alachiq/node_modules/co/index.js:69:7)
    at /Applications/XAMPP/xamppfiles/htdocs/alachiq/node_modules/co/index.js:54:5
    at co (/Applications/XAMPP/xamppfiles/htdocs/alachiq/node_modules/co/index.js:50:10)
    at toPromise (/Applications/XAMPP/xamppfiles/htdocs/alachiq/node_modules/co/index.js:118:63)
    at next (/Applications/XAMPP/xamppfiles/htdocs/alachiq/node_modules/co/index.js:99:29)
pishguy commented 7 years ago

@thetutlage could you find whats problem? i can't find

thetutlage commented 7 years ago

It's because the field name in the table is channel_type_id whereas Adonis default foreign key is channels_type_id

Redefine your relation as

channelsTypeId() {
    return this.belongsTo('App/Model/ChannelsType', 'id', 'channel_type_id')
}
pishguy commented 7 years ago

@thetutlage Thanks, but after that my relation array is empty and i dont have channelsType values on result:

AlachiqChannel {
  attributes: 
   { id: 1,
     channelName: 'خبر و خبرگزاریهای رسمی',
     channelLink: 'cafemaku/sajjadmajjad',
     channelDescription: 'کانالهای خبرگزاری رسمی کشور',
     channelImageFileName: 'boldt.jpg',
     channelAvatarFileName: '34.jpg',
     channelState: 0,
     channel_type_id: 1,
     channelOwnerUserId: 0,
     fileServerUrlId: 0,
     created_at: 2016-09-01T15:04:12.000Z,
     updated_at: 2016-09-01T15:04:12.000Z },
  original: 
   { id: 1,
     channelName: 'خبر و خبرگزاریهای رسمی',
     channelLink: 'cafemaku/sajjadmajjad',
     channelDescription: 'کانالهای خبرگزاری رسمی کشور',
     channelImageFileName: 'boldt.jpg',
     channelAvatarFileName: '34.jpg',
     channelState: 0,
     channel_type_id: 1,
     channelOwnerUserId: 0,
     fileServerUrlId: 0,
     created_at: 2016-09-01T15:04:12.000Z,
     updated_at: 2016-09-01T15:04:12.000Z },
  transaction: null,
  relations: {},
  frozen: false,
  eagerLoad: 
   EagerLoad {
     withRelations: [],
     withNestedRelations: {},
     relationsScope: {},
     nestedRelationsScope: {} } }
muxahuk commented 7 years ago

@tux-world You need to do: const chanels = yield channel.channelsTypeId().fetch()

lock[bot] commented 4 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.