Wrong model name

Hi,
I just created my forest project, everything is going well. But when I create a relation between user and userdata I get this error :

[forest] 🌳🌳🌳  Unexpected error: Unknown column 'userDatum.id' in 'order clause'
{
  "name": "SequelizeDatabaseError",
  "parent": {
    "code": "ER_BAD_FIELD_ERROR",
    "errno": 1054,
    "sqlState": "42S22",
    "sqlMessage": "Unknown column 'userDatum.id' in 'order clause'",
    "sql": "SELECT `user`.`id`, `user`.`companyId`, `user`.`email`, `user`.`password`, `user`.`indicMobile`, `user`.`mobile`, `user`.`emailValidated`, `user`.`resetPassword`, `user`.`resetPasswordExp`, `user`.`lastLogin`, `user`.`createdAt`, `user`.`updatedAt`, `userDatum`.`userId` AS `userDatum.userId`, `userDatum`.`createdAt` AS `userDatum.createdAt` FROM `User` AS `user` LEFT OUTER JOIN `UserData` AS `userDatum` ON `user`.`id` = `userDatum`.`userId` ORDER BY `userDatum`.`id` DESC LIMIT 0, 15;"
  },
  "original": {
    "code": "ER_BAD_FIELD_ERROR",
    "errno": 1054,
    "sqlState": "42S22",
    "sqlMessage": "Unknown column 'userDatum.id' in 'order clause'",
    "sql": "SELECT `user`.`id`, `user`.`companyId`, `user`.`email`, `user`.`password`, `user`.`indicMobile`, `user`.`mobile`, `user`.`emailValidated`, `user`.`resetPassword`, `user`.`resetPasswordExp`, `user`.`lastLogin`, `user`.`createdAt`, `user`.`updatedAt`, `userDatum`.`userId` AS `userDatum.userId`, `userDatum`.`createdAt` AS `userDatum.createdAt` FROM `User` AS `user` LEFT OUTER JOIN `UserData` AS `userDatum` ON `user`.`id` = `userDatum`.`userId` ORDER BY `userDatum`.`id` DESC LIMIT 0, 15;"
  },
  "sql": "SELECT `user`.`id`, `user`.`companyId`, `user`.`email`, `user`.`password`, `user`.`indicMobile`, `user`.`mobile`, `user`.`emailValidated`, `user`.`resetPassword`, `user`.`resetPasswordExp`, `user`.`lastLogin`, `user`.`createdAt`, `user`.`updatedAt`, `userDatum`.`userId` AS `userDatum.userId`, `userDatum`.`createdAt` AS `userDatum.createdAt` FROM `User` AS `user` LEFT OUTER JOIN `UserData` AS `userDatum` ON `user`.`id` = `userDatum`.`userId` ORDER BY `userDatum`.`id` DESC LIMIT 0, 15;",
  "stack": "SequelizeDatabaseError: Unknown column 'userDatum.id' in 'order clause'\n    at Query.formatError (/Users/bvelluet/Git/Alfred/node_modules/sequelize/lib/dialects/mysql/query.js:243:16)\n    at Query.handler [as onResult] (/Users/bvelluet/Git/Alfred/node_modules/sequelize/lib/dialects/mysql/query.js:50:23)\n    at Query.execute (/Users/bvelluet/Git/Alfred/node_modules/mysql2/lib/commands/command.js:30:14)\n    at Connection.handlePacket (/Users/bvelluet/Git/Alfred/node_modules/mysql2/lib/connection.js:425:32)\n    at PacketParser.onPacket (/Users/bvelluet/Git/Alfred/node_modules/mysql2/lib/connection.js:75:12)\n    at PacketParser.executeStart (/Users/bvelluet/Git/Alfred/node_modules/mysql2/lib/packet_parser.js:75:16)\n    at Socket.<anonymous> (/Users/bvelluet/Git/Alfred/node_modules/mysql2/lib/connection.js:82:25)\n    at Socket.emit (node:events:369:20)\n    at addChunk (node:internal/streams/readable:313:12)\n    at readableAddChunk (node:internal/streams/readable:288:9)\n    at Socket.Readable.push (node:internal/streams/readable:227:10)\n    at TCP.onStreamRead (node:internal/stream_base_commons:190:23)"
}

Screenshot 2021-05-03 at 19.08.40

Screenshot 2021-05-03 at 19.08.47

Impossible to find where the “userDatum” comes from, it is regenerated each time in the schema.json

Thank’s

Hello @Benjamin,

Not sure to totally understand what’s going on. Maybe a cleaner relationship definition should help understand.

   User.hasOne(models.userData, {
      foreignKey: {
        name: 'userIdKey',
        field: 'userId',
      },
      as: 'userData',
    });
    UserData.belongsTo(models.user, {
      foreignKey: {
        name: 'userIdKey',
        field: 'userId',
      },
      as: 'user',
    });

Also, I would recommend you to read the Sequelize documentation for more information.

Can you tell me if it helps. Thank you.
Morgan

Hi @morganperre,

Good ! It’s working fine now.
Thank you

1 Like

It was a pleasure. Have a great experience on our admin panel.