Home > Software engineering >  Unknown error sequelize when adding new table column
Unknown error sequelize when adding new table column

Time:07-02

I have the following models.js:

const sequelize = require('../db')
const {DataTypes} = require('sequelize')

const User = sequelize.define('user', {
    id: {type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true},
    email: {type: DataTypes.STRING, unique: true},
    password: {type: DataTypes.STRING, unique: true},
    role: {type: DataTypes.STRING, defaultValue: "USER"}
})


const Basket = sequelize.define('basket', {
    id: {type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true}
})

const BasketDevice = sequelize.define('basket_device', {
    id: {type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true},
}) 

const Device = sequelize.define('device', {
    id: {type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true},
    name: {type: DataTypes.STRING, unique: true, allowNull: false},
    price: {type: DataTypes.INTEGER, allowNull: false},
    rating: {type: DataTypes.INTEGER, allowNull: false, defaultValue: 0},
    img: {type: DataTypes.STRING, unique: true, allowNull: false},
}) 

There contains User, Device, Basket for User, and basketDevice for many-to-many relationship.

And this controller:

async getOne(req, res, next){
      const basket = await Basket.findOne({
          where: {userId: req.user.id},
          include: {
              model: BasketDevice,
              include: {
                  model: Device
              }
          }
      })    
      return res.json(basket)
  }

And this correctly working(basket JSON):

{
    "id": 11,
    "createdAt": "2022-06-27T23:15:28.431Z",
    "updatedAt": "2022-06-27T23:15:28.431Z",
    "userId": 11,
    "basket_devices": [
        {
            "id": 6,
            "createdAt": "2022-06-27T23:15:40.782Z",
            "updatedAt": "2022-06-27T23:15:40.782Z",
            "basketId": 11,
            "deviceId": 1,
            "device": {
                "id": 1,
                "name": "12 pro",
                "price": 10000,
                "rating": 0,
                "img": "43049c4e-e50b-4aec-b6a9-de7fec272f25.jpg",
                "createdAt": "2022-06-18T21:10:18.036Z",
                "updatedAt": "2022-06-18T21:10:18.036Z",
                "typeId": 2,
                "brandId": 2
            }
        }
    ]
}

But if i add new column in basketDevice table, which store count of devices:

   const BasketDevice = sequelize.define('basket_device', {
    id: {type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true},
    count: {type: DataTypes.INTEGER, allowNull: false, defaultValue: 0},
}) 

I get following error:

Executing (default): SELECT "basket".*, "basket_devices"."id" AS "basket_devices.id", "basket_devices"."count" AS "basket_devices.count", "basket_devices"."createdAt" AS "basket_devices.createdAt", "basket_devices"."updatedAt" AS "basket_devices.updatedAt", "basket_devices"."basketId" AS "basket_devices.basketId", "basket_devices"."deviceId" AS "basket_devices.deviceId", "basket_devices->device"."id" AS "basket_devices.device.id", "basket_devices->device"."name" AS "basket_devices.device.name", "basket_devices->device"."price" AS "basket_devices.device.price", "basket_devices->device"."rating" AS "basket_devices.device.rating", "basket_devices->device"."img" AS "basket_devices.device.img", "basket_devices->device"."createdAt" AS "basket_devices.device.createdAt", "basket_devices->device"."updatedAt" AS "basket_devices.device.updatedAt", "basket_devices->device"."typeId" AS "basket_devices.device.typeId", "basket_devices->device"."brandId" AS "basket_devices.device.brandId" FROM (SELECT "basket"."id", "basket"."createdAt", "basket"."updatedAt", "basket"."userId" FROM "baskets" AS "basket" WHERE "basket"."userId" = 11 GROUP BY "basket"."id" LIMIT 1) AS "basket" LEFT OUTER JOIN "basket_devices" AS "basket_devices" ON "basket"."id" = "basket_devices"."basketId" LEFT OUTER JOIN "devices" AS "basket_devices->device" ON "basket_devices"."deviceId" = "basket_devices->device"."id";
node:internal/process/promises:265
            triggerUncaughtException(err, true /* fromPromise */);
            ^

Error
    at Query.run (D:\JavaScript\testNodeReact\server\node_modules\sequelize\lib\dialects\postgres\query.js:50:25)
    at D:\JavaScript\testNodeReact\server\node_modules\sequelize\lib\sequelize.js:311:28
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async PostgresQueryInterface.select (D:\JavaScript\testNodeReact\server\node_modules\sequelize\lib\dialects\abstract\query-interface.js:407:12)
    at async Function.findAll (D:\JavaScript\testNodeReact\server\node_modules\sequelize\lib\model.js:1134:21)
    at async Function.findOne (D:\JavaScript\testNodeReact\server\node_modules\sequelize\lib\model.js:1228:12)
    at async getOne (D:\JavaScript\testNodeReact\server\controllers\basketController.js:16:24) {
  name: 'SequelizeDatabaseError',
  parent: error: column "basket.id " must appear in the GROUP BY clause or be used in an aggregate function

I dont understand why this error occurs. Thank you in advance.

CodePudding user response:

Fixed, needed rename 'count', because it is sql keyword

  • Related