Repository: sequelize/sequelize

Issue #11112: Model:destroy does not use the real 'field' name.
Created: 2019-06-25T20:07:44Z
What are you doing?
I't trying to delete some records from a table
// My model definition
class MyModel extends Model {}
MyModel.init({
  userEmail: {
    type: DataTypes.STRING,
    field: 'user_email',
    primaryKey: true,
  },
  brandId: {
    type: DataTypes.INTEGER,
    field: 'brand_id',
    primaryKey: true,
  },
  description: {
    type: DataTypes.TEXT,
    allowNull: true,
  },
}, {
  sequelize,
  modelName: 'myModel',
  tableName: 'my_table',
  underscored: true,
  timestamps: false,
  version: false,
});

// How I used to run the query before v5
MyModel.destroy({ transaction, where: { userEmail:  'some email'} });
What do you expect to happen?
get this query:
DELETE FROM my_table WHERE "user_email"='some email'
What is actually happening?
I'm getting this other one:
DELETE FROM my_table WHERE "userEmail"='some email' producing column "userEmail" does not exist at character X
Environment
Dialect:

 mysql
 postgres
 sqlite
 mssql
 any
Dialect library version: 7.8.0
Database version: 11.2
Sequelize version: tested with v5.8.12 and v6.0.0
Node Version: 10
OS: macOS

Tested with latest release:

 No
 Yes, specify that version: 5.8.12 and 6.0.0

Duplicate issue #14256: camel case to snake case issue in destroy() method call for an instance
Created: 2022-03-16T13:44:57Z
Issue Creation Checklist
[ X] I have read the contribution guidelines
Issue Description
I am using with postgresal dialact, sequelize version is 6.14.0 for nodejs
The model is defined with underscored: true
findOne() method correctly convert the field name from camel case to snake case and return the instance.
when I call destroy() of the instance, it execute query with camel case field which case the error.
on findOne() method call:
Executing (default): SELECT "token", "user_id" AS "userId", "token_type" AS "tokenType", "expires", "blocklisted", "organization_id" AS "organizationId", "created_at" AS "createdAt", "updated_at" AS "updatedAt" FROM "tokens" 
AS "token" WHERE "token"."token" = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOjEsImlhdCI6MTY0NzI3Njc5NiwiZXhwIjoxNjQ5ODY1MTk2LCJ0eXBlIjoicmVmcmVzaCJ9.AVaZzOjALodjIeSud4lpSHSyb76nLN6h8PixtIMx61M' AND "token"."blocklisted" 
= false AND "token"."token_type" = 'refresh';
token {
  dataValues: {
    token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOjEsImlhdCI6MTY0NzI3Njc5NiwiZXhwIjoxNjQ5ODY1MTk2LCJ0eXBlIjoicmVmcmVzaCJ9.AVaZzOjALodjIeSud4lpSHSyb76nLN6h8PixtIMx61M',
    userId: 1,
    tokenType: 'refresh',
    expires: 2022-04-13T13:53:16.508Z,
    blocklisted: false,
    organizationId: 1,
    createdAt: 2022-03-14T15:53:16.509Z,
    updatedAt: 2022-03-14T15:53:16.509Z
  },
  _previousDataValues: {
    token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOjEsImlhdCI6MTY0NzI3Njc5NiwiZXhwIjoxNjQ5ODY1MTk2LCJ0eXBlIjoicmVmcmVzaCJ9.AVaZzOjALodjIeSud4lpSHSyb76nLN6h8PixtIMx61M',
    userId: 1,
    tokenType: 'refresh',
    expires: 2022-04-13T13:53:16.508Z,
    blocklisted: false,
    organizationId: 1,
    createdAt: 2022-03-14T15:53:16.509Z,
    updatedAt: 2022-03-14T15:53:16.509Z
  },
  uniqno: 1,
  _changed: Set(0) {},
  _options: {
    isNewRecord: false,
    _schema: null,
    _schemaDelimiter: '',
    raw: true,
    attributes: [
      'token',
      'userId',
      'tokenType',
      'expires',
      'blocklisted',
      'organizationId',
      'createdAt',
      'updatedAt'
    ]
  },
  isNewRecord: false
}

on destroy() method call:
Executing (default): DELETE FROM "tokens" WHERE "token" = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOjEsImlhdCI6MTY0NzI3Njc5NiwiZXhwIjoxNjQ5ODY1MTk2LCJ0eXBlIjoicmVmcmVzaCJ9.AVaZzOjALodjIeSud4lpSHSyb76nLN6h8PixtIMx61M' AND "tokenType" = 'refresh' AND "blocklisted" = false
Error
    at Query.run (E:\workspace\p4w\p4wserver\node_modules\sequelize\lib\dialects\postgres\query.js:50:25)
    at E:\workspace\p4w\p4wserver\node_modules\sequelize\lib\sequelize.js:313:28
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async PostgresQueryInterface.delete (E:\workspace\p4w\p4wserver\node_modules\sequelize\lib\dialects\abstract\query-interface.js:382:12)
    at async model.destroy (E:\workspace\p4w\p4wserver\node_modules\sequelize\lib\model.js:2542:16)
    at async Object.logout (E:\workspace\p4w\p4wserver\services\auth.service.js:34:3)
    at async E:\workspace\p4w\p4wserver\controllers\auth.controller.js:19:3 {
  name: 'SequelizeDatabaseError',
  parent: error: column "tokenType" does not exist
      at Parser.parseErrorMessage (E:\workspace\p4w\p4wserver\node_modules\pg-protocol\dist\parser.js:287:98)
      at Parser.handlePacket (E:\workspace\p4w\p4wserver\node_modules\pg-protocol\dist\parser.js:126:29)
      at Parser.parse (E:\workspace\p4w\p4wserver\node_modules\pg-protocol\dist\parser.js:39:38)
      at Socket.<anonymous> (E:\workspace\p4w\p4wserver\node_modules\pg-protocol\dist\index.js:11:42)
      at Socket.emit (node:events:390:28)
      at addChunk (node:internal/streams/readable:315:12)
      at readableAddChunk (node:internal/streams/readable:289:9)
      at Socket.Readable.push (node:internal/streams/readable:228:10)
      at TCP.onStreamRead (node:internal/stream_base_commons:199:23) {
    length: 237,
    severity: 'ERROR',
    code: '42703',
    detail: undefined,
    hint: 'Perhaps you meant to reference the column "tokens.token_type".',
    position: '206',
    internalPosition: undefined,
    internalQuery: undefined,
    where: undefined,
    schema: undefined,
    table: undefined,
    column: undefined,
    dataType: undefined,
    constraint: undefined,
    file: 'd:\\pginstaller_13.auto\\postgres.windows-x64\\src\\backend\\parser\\parse_relation.c',
    line: '3513',
    routine: 'errorMissingColumn',
    sql: `DELETE FROM "tokens" WHERE "token" = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOjEsImlhdCI6MTY0NzI3Njc5NiwiZXhwIjoxNjQ5ODY1MTk2LCJ0eXBlIjoicmVmcmVzaCJ9.AVaZzOjALodjIeSud4lpSHSyb76nLN6h8PixtIMx61M' AND "tokenType" = 'refresh' AND "blocklisted" = false`,
    parameters: undefined
  },
  original: error: column "tokenType" does not exist
      at Parser.parseErrorMessage (E:\workspace\p4w\p4wserver\node_modules\pg-protocol\dist\parser.js:287:98)
      at Parser.handlePacket (E:\workspace\p4w\p4wserver\node_modules\pg-protocol\dist\parser.js:126:29)
      at Parser.parse (E:\workspace\p4w\p4wserver\node_modules\pg-protocol\dist\parser.js:39:38)
      at Socket.<anonymous> (E:\workspace\p4w\p4wserver\node_modules\pg-protocol\dist\index.js:11:42)
      at Socket.emit (node:events:390:28)
      at addChunk (node:internal/streams/readable:315:12)
      at readableAddChunk (node:internal/streams/readable:289:9)
      at Socket.Readable.push (node:internal/streams/readable:228:10)
      at TCP.onStreamRead (node:internal/stream_base_commons:199:23) {
    length: 237,
    severity: 'ERROR',
    code: '42703',
    detail: undefined,
    **hint: 'Perhaps you meant to reference the column "tokens.token_type".',**
    position: '206',
    internalPosition: undefined,
    internalQuery: undefined,
    where: undefined,
    schema: undefined,
    table: undefined,
    column: undefined,
    dataType: undefined,
    constraint: undefined,
    file: 'd:\\pginstaller_13.auto\\postgres.windows-x64\\src\\backend\\parser\\parse_relation.c',
    line: '3513',
    routine: 'errorMissingColumn',
    sql: `DELETE FROM "tokens" WHERE "token" = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOjEsImlhdCI6MTY0NzI3Njc5NiwiZXhwIjoxNjQ5ODY1MTk2LCJ0eXBlIjoicmVmcmVzaCJ9.AVaZzOjALodjIeSud4lpSHSyb76nLN6h8PixtIMx61M' AND "tokenType" = 'refresh' AND "blocklisted" = false`,
    parameters: undefined
  },
  sql: `DELETE FROM "tokens" WHERE "token" = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOjEsImlhdCI6MTY0NzI3Njc5NiwiZXhwIjoxNjQ5ODY1MTk2LCJ0eXBlIjoicmVmcmVzaCJ9.AVaZzOjALodjIeSud4lpSHSyb76nLN6h8PixtIMx61M' AND "tokenType" 
= 'refresh' AND "blocklisted" = false`,
  parameters: {}
}

Kindly help me in fixing this issue.

Additional context
Add any other context or screenshots about the issue here.
Issue Template Checklist

Is this issue dialect-specific?

 No. This issue is relevant to Sequelize as a whole.
 Yes. This issue only applies to the following dialect(s): XXX, YYY, ZZZ
 I don't know.

Would you be willing to resolve this issue by submitting a Pull Request?


 Yes, I have the time and I know how to start.
 Yes, I have the time but I don't know how to start, I would need guidance.
 No, I don't have the time, although I believe I could do it if I had the time...
 No, I don't have the time and I wouldn't even know how to start.

Closing comment (2022-03-16T14:00:39Z): Hello,
Please use Code Blocks to make your issue readable: https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/creating-and-highlighting-code-blocks
Your issue may be the same as #11112