Sequelize Composite Foreign Key

前端 未结 1 1045
醉话见心
醉话见心 2021-02-07 12:39

I have a database with the following tables:

CREATE TABLE IF NOT EXISTS `app_user` (
  `user_id` INT NOT NULL,
  `user_name` VARCHAR(45) NOT NULL,
  PRIMARY KEY          


        
相关标签:
1条回答
  • 2021-02-07 12:55

    Now Sequelize doesn't support composite foreign keys. This creates several problems.

    1. When Sequelize creates a table, the table definition does not have a composite FK.
      To solve this problem I use the afterSync hook on the model and a function that adds a FK to the table if it does not exist. Example code.
    2. When I use the findAll method with include such model, I use the include[].on option of the findAll method. Or if you don't use as many joins as I do, you can use scope when creating an association (see).
    0 讨论(0)
提交回复
热议问题