Objection.js: model.query().insert() doesn't accepts camelcase.

Created on 11 Nov 2018  路  6Comments  路  Source: Vincit/objection.js

In User Model:

 static columnNameMappers = snakeCaseMappers();

Query:

try {
  const user = await User.query().insert(firstName, lastName});
  res.send({ user, message: 'User profile is created' });
} catch (error) {
  return next(new HttpError(400, error.message));
}

I'm getting error
"first_name: is a required property, last_name: is a required property"

If I change to await User.query().insert(first_name: firstName, last_name: lastName});. It works

All 6 comments

Also, one strange thing is happening that if I use .sql(). I'm getting query like this 'select "user".* from "user"'

You have defined your jsonSchema using snake_case, so obviously it doesn't accept camel case.

jsonSchema is not the database schema, but the "input" schema for creating model instances.

After trying a lot I added these two lines

 $parseJson(json, opt) { // De-Camelize key when creates Model Object
    return super.$parseJson(decamelizeKeys(json), opt);
  }


  $formatJson(json) { //Camelize key for  Model -> Json
    return camelizeKeys(super.$formatJson(json));
  }

Hi @koskimas, thanks for reply.. I found the issue.. It was typo in Model Json Schema like an idiot. I typed the required property as snake case.

image

I removed the two above two functions.. It's working.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

louis-etne picture louis-etne  路  4Comments

zuck picture zuck  路  4Comments

Ahlid picture Ahlid  路  3Comments

nicolaracco picture nicolaracco  路  3Comments

mycahjay-nms picture mycahjay-nms  路  4Comments