Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

this.belongsTo is not a function #3

Closed
brenolf opened this issue Oct 16, 2015 · 16 comments
Closed

this.belongsTo is not a function #3

brenolf opened this issue Oct 16, 2015 · 16 comments

Comments

@brenolf
Copy link

brenolf commented Oct 16, 2015

I'm trying the basic example shown in the docs but I'm getting the title error. The code I'm running is as follows:

'use strict'

const Lucid = use('Lucid')

class Opinion extends Lucid {
  user () {
    return this.belongsTo('App/Model/User')
  }
}

module.exports = Opinion

And using like:

let opinion = new Opinion
opinion.user().associate(user)

Should I create it before associating?

@thetutlage
Copy link
Member

how have you created the user variable ?

@brenolf
Copy link
Author

brenolf commented Oct 16, 2015

Forgot that! Here it is:

let user

try {
  user = yield User.find(request.param('id'))
} catch (e) {
  response.notFound()
  return
}

let opinion = new Opinion

opinion.liked = request.input('liked')

try {
  opinion.user().associate(user)

  yield opinion.create()

  response.created()
} catch (e) {
  response.internalServerError()
}

@thetutlage
Copy link
Member

also can you tell what error you get ?

@brenolf
Copy link
Author

brenolf commented Oct 16, 2015

I get the title's error: this.belongsTo is not a function on return this.belongsTo('App/Model/User') line

@thetutlage
Copy link
Member

This is wierd , let me reproduce it

@thetutlage
Copy link
Member

All worked great for me , sharing my code

const user = yield User.find(1)

if(Object.keys(user.attributes).length === 0){
  return 'Not found'
}

const opinion = new Opinion
opinion.user().associate(user)

const created = yield opinion.create()
if(created && created[0]) {
  return 'All Good'
}

return 'Sorry unable to save your opinion'

Couple of things to note here

  1. Lucid only throw error when something goes wrong , like some field in the table does not exists and so on. So there is no need to have multiple try/catch blocks as the final request handler will handle these exceptions for you.
  2. Right now you have to the use the dirty way to check whether User.find(someId) worked or not by checking the length of attributes on user instance, but as i am working to improve the entire framework later these types of error checking will be easier.

@brenolf
Copy link
Author

brenolf commented Oct 16, 2015

Weird. I keep having the same error. Any ideas on what could it be?

Thanks for the notes btw.

@thetutlage
Copy link
Member

what version of lucid u using ?

@brenolf
Copy link
Author

brenolf commented Oct 16, 2015

1.0.2

@thetutlage
Copy link
Member

Ohhhh , relationships have been introduced in v1.0.3. Solved :)

@brenolf
Copy link
Author

brenolf commented Oct 16, 2015

Just updated here and now it works! My bad, sorry about that.

@brenolf brenolf closed this as completed Oct 16, 2015
@thetutlage
Copy link
Member

No issues , docs needs to have upgrade section

@shanthakumarf22
Copy link

on ^6.1.3 still having this issue. looks like its related to something else.

@herickwilke
Copy link

on ^6.1.3 still having this issue. looks like its related to something else.

I'm having the same issue.

"@adonisjs/lucid": "^6.1.3",

@zelucena
Copy link

zelucena commented Jul 28, 2020

I've just had this issue with two relationships in which none were named 'foreigntable_primarykey'; both were just 'foreigntable'. I tried creating a new column in my DB with the expected naming system and it worked.
Eg: this.belongsTo('App/Models/Client', 'client', 'id') -> won't work
this.belongsTo('App/Models/Client', 'client_id', 'id') -> works properly

Update:
Actual error message I wrap with try..catch:
"select * from client where client.id in ((select * from client), (select * from client), (select * from client), (select * from client), (select * from client), (select * from client), (select * from client), (select * from client), (select * from client), (select * from client)) - ER_OPERAND_COLUMNS: Operand should contain 1 column(s)"

The is a big problem for me, as I am working with legacy code alongside sharing this database and I should not modify the current database structure.

@Benez11
Copy link

Benez11 commented May 13, 2022

@thetutlage

I've just had this issue with two relationships in which none were named 'foreigntable_primarykey'; both were just 'foreigntable'. I tried creating a new column in my DB with the expected naming system and it worked. Eg: this.belongsTo('App/Models/Client', 'client', 'id') -> won't work this.belongsTo('App/Models/Client', 'client_id', 'id') -> works properly

Update: Actual error message I wrap with try..catch: "select * from client where client.id in ((select * from client), (select * from client), (select * from client), (select * from client), (select * from client), (select * from client), (select * from client), (select * from client), (select * from client), (select * from client)) - ER_OPERAND_COLUMNS: Operand should contain 1 column(s)"

The is a big problem for me, as I am working with legacy code alongside sharing this database and I should not modify the current database structure.

@thetutlage I'm in a similar condition. Any resolution for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants