Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 不带外键的关联和表中的make join_Mysql_Model_Sequelize.js_Associations - Fatal编程技术网

Mysql 不带外键的关联和表中的make join

Mysql 不带外键的关联和表中的make join,mysql,model,sequelize.js,associations,Mysql,Model,Sequelize.js,Associations,我正在Node+sequelize+mysql上工作。在我的代码中,所有的东西都工作得很好。现在我需要在列表页面中添加搜索字段。在这个搜索页面中,会显示用户的名字、姓氏、公司名称和状态。我从一个表中获得所有数据,但公司来自“公司”表。在用户表中有公司id。我无法关联公司id。现在我也想从公司名称中搜索。如何添加包含而不关联 const { User, Company } = require("../models"); if(query.trim() != "") { [er

我正在Node+sequelize+mysql上工作。在我的代码中,所有的东西都工作得很好。现在我需要在列表页面中添加搜索字段。在这个搜索页面中,会显示用户的名字、姓氏、公司名称和状态。我从一个表中获得所有数据,但公司来自“公司”表。在用户表中有公司id。我无法关联公司id。现在我也想从公司名称中搜索。如何添加包含而不关联

const { User, Company } = require("../models"); 

if(query.trim() != "") { 
        [err, users] = await to(User.findAll({
            where : {[Op.or] : {first_name:{ [Op.like]: '%'+query.trim()+'%' },last_name:{ [Op.like]: '%'+query.trim()+'%' }}}, 
            include : [
                {
                    model:Company,
                    where : {company_name:{ [Op.like]: '%'+query.trim()+'%' }}
                }],
            limit: 5,
            offset: 0,
            order: [[id, ASC]]
        }));
        console.log('err ----------------',err);
        console.log(users);
}
我从上面的代码中得到以下错误:

err ---------------- { filename:
   '/var/www/html/fullproject-name/backend/node_modules/sequelize/lib/model.js',
  line: 583,
  row: 13,
  message: 'Company is not associated to User!',
  type: 'SequelizeEagerLoadingError',
  stack:
   'SequelizeEagerLoadingError: Company is not associated to User!\n    at Function._getIncludedAssociation..... }
用户型号代码:

module.exports = (sequelize, DataTypes) => {
    var Model = sequelize.define('User', { 
        email: {type: DataTypes.STRING, allowNull: true, unique: true, validate: { isEmail: {msg: "Phone number invalid."} }}, 
        company_id: DataTypes.INTEGER,
        first_name: DataTypes.STRING,
        last_name: DataTypes.STRING, 
        active:DataTypes.INTEGER, 
    });

}
公司型号代码:

module.exports = (sequelize, DataTypes) => {
  var Model = sequelize.define('Company', {
    company_name: DataTypes.STRING, 
    company_attachment: DataTypes.STRING,
  });
   }
您需要与对方保持联系,以便能够使用联接一起查询它们。在下面的示例中,我为
用户
公司
添加了一些关联。我定义的关系(您可以更改)是一对多。一个用户属于一家公司;一家公司有很多用户

此关联将在
用户
模型上放置
公司id
外键

const models = require("./models.js") // Update if necessary
module.exports = (sequelize, DataTypes) => {
  var User = sequelize.define("User", { // Changed from Model to User
    email: {
      type: DataTypes.STRING,
      allowNull: true,
      unique: true,
      validate: { isEmail: { msg: "Phone number invalid." } }
    },
    company_id: DataTypes.INTEGER,
    first_name: DataTypes.STRING,
    last_name: DataTypes.STRING,
    active: DataTypes.INTEGER
  });

  User.belongsTo(models.Company); // User to Company association
};

const models = require("./models.js") // Update if necessary
module.exports = (sequelize, DataTypes) => {
  var Company = sequelize.define("Company", { // Changed from Model to Company
    company_name: DataTypes.STRING,
    company_attachment: DataTypes.STRING
  });

  Company.hasMany(models.User); // Company to User association
};