How to use include to obtain multi-table data when egg-Sequelize has no foreign key relationship

problem description

as mentioned, when egg-Sequelize has no foreign key relationship (created with reference), it is not possible to query data related to multiple tables using include. Also ask the gods to advise
or after using association, you can bypass this problem without generating foreign keys when using sync to create a database.

the environmental background of the problems and what methods you have tried

  1. Thank you for the article related API documents , keyword search (" unconstrained foreign key references ") to learn that using reference can avoid creating foreign key relationships, try to use app.sync to generate libraries, find no relationship, project rerun, and find that you still need to point to the relationship chain between Model,
  2. .
  3. if you use association, instead of reference, the database foreign key will be created. No, no, no.

related codes

/app/model/user.js

    ...
    User.associate = () =>{
        User.hasMany(app.model.UserRole); // reference
    }
    ...

what result do you expect? What is the error message actually seen?

using sync to generate a database does not generate foreign keys and can include multiple model queries when it can be queried.

Thank you!

Nov.04,2021

has been resolved. After careful inspection, it is found that the foreign key created is connected when deleting and updating. With this keyword to check the original English api, you find that there is an attribute of constraints. When creating a relationship such as hasOne/belongsTo, you can set this attribute to false in option.

Menu