sequelize / sequelize/sequelize

Sequelize considering camelCase'd foreign keys as fields different than snake_case'd on database (even with 'underscored: true' option)

Open
#16,689 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

pending-approval
Dominant language
TypeScript
Stars
30.4k
Forks
4.3k
Avg merge
1d 6h
Merged PRs (30d)
68

Description

I noticed something quite strange - perhaps a bug or just an intuitive behavior on the way Sequelize works - when dealing with foreign key names in sequelize models:

I've got a junction table model which foreign keys are defined on camelCase (in the dbConfig i used underscore: true), but the model name is defined on PascalCase:

export default class CourseModality extends Model {
  static init(sequelize) {
    super.init(
      {
        courseId: {
          type: DataTypes.INTEGER,
          allowNull: false,
        },
        modalityId: {
          type: DataTypes.INTEGER,
          allowNull: false,
        },
      },
      { sequelize, timestamps: false },
    );

    return this;
  }
}

When i tried to retrieve the results of a query on the model using where clause with the foreign keys as "courseId" and "modalityId" (camelCase) i got the SqlError "Column 'course_id' cannot be null". And when i tried as "course_id" and "modality_id" (snake_case), i got the SequelizeValidationError error CourseModality.courseId cannot be null. And what is quite strange is that, when i tried using the foreign keys as "CourseId" and "ModalityId" (PascalCase), i received another SequelizeValidationError: CourseModality.courseId cannot be null.

It seems that Sequelize is considering "courseId" and "modalityId" from the model as fields different and totally independent from "course_id" and "modality_id" on database that i created and defined with sequelize migrations (i'm using MYSQL and i checked that the fields courseId and modalityId does not exist on db).

On querying all courses on db including all modalities associated with them, that's the JSON result:

{
    "id": 2,
    "name": "Design",
    "category": "Arquitetura e Urbanismo & Design",
    "durationSem": 6,
    "degree": "Bachelor",
    "createdAt": "2023-10-24T21:27:30.000Z",
    "updatedAt": "2023-10-24T21:27:30.000Z",
    "modalities": [
        {
            "name": "On-campus",
            "CourseModality": {
                "courseId": 2,
                "modalityId": 1,
                "CourseId": 2,
                "ModalityId": 1
            }
        }
    ]
},

(i'm using the LTS version of sequelize).

The way solved the problem was changing all my models' names to lowercase (all were in UpperCase) and mentioning the foreign keys as camelCase, but i don't know for sure how sequelize works on namings and aliases related to foreign keys, it seems that this is a potential bug or just a unexpected behavior...

The query result after the changes:

{
    "id": 2,
    "name": "Design",
    "category": "Arquitetura e Urbanismo & Design",
    "durationSem": 6,
    "degree": "Bachelor",
    "createdAt": "2023-10-24T21:27:30.000Z",
    "updatedAt": "2023-10-24T21:27:30.000Z",
    "modalities": [
        {
            "name": "On-campus",
            "CourseModality": {
                "courseId": 2,
                "modalityId": 1
            }
        }
    ]
},

link to the repository of the project where all the code mentioned can be found:
https://github.com/augusto-dmh/rest-api-faculdade

Originally posted by @augusto-dmh in https://github.com/sequelize/sequelize/issues/12366#issuecomment-1779451795

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked repository's CourseModality model, its database migration, and the Sequelize configuration using the underscored option. Reproduce the camelCase, snake_case, and PascalCase queries against MySQL, then compare the generated field mappings and query results; done means the expected foreign-key mapping is clarified or a reproducible defect is isolated.

Written by the indexing model from the issue text.

Assessment

Tech stack
mysql, node.js, typescript
Domain
backend, databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.