Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.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 如何执行Sequelize连接_Mysql_Node.js_Express_Sequelize.js - Fatal编程技术网

Mysql 如何执行Sequelize连接

Mysql 如何执行Sequelize连接,mysql,node.js,express,sequelize.js,Mysql,Node.js,Express,Sequelize.js,我有一个运行在nodejs中的Express应用程序,并使用Sequelize处理MySQL数据库。在我的数据库逻辑中,聊天室和用户聊天室之间有一对多的关系。当我尝试使用以下代码执行连接时: var userId = 1; db.ChatRoom.findAll({ include: [{ model: db.UserChatRoom, where: { ucrIdChatUser:user

我有一个运行在nodejs中的Express应用程序,并使用Sequelize处理MySQL数据库。在我的数据库逻辑中,聊天室和用户聊天室之间有一对多的关系。当我尝试使用以下代码执行连接时:

var userId = 1;

db.ChatRoom.findAll({ include: [{
                model: db.UserChatRoom,
                where: {
                    ucrIdChatUser:userId 
                },
                required: false
            }]})
我在控制台上收到以下错误消息:

Unhandled rejection Error: UserChatRoom is not associated to ChatRoom!
    at validateIncludedElement 
我想让加入查询工作,并将感谢帮助。我尝试在index.js的末尾强制执行关联。当应用程序启动并从Sequalize网站获取时,此脚本将加载此文件夹中的模型

注意:UserChatRoom.ucrIdChatRoom是UserChat.idChatRoom的外键

型号:

module.exports = function(sequelize, DataType){
    var ChatRoom = sequelize.define("ChatRoom",{
        idChatUser: {
            type: DataType.INTEGER,
            autoIncrement: true,
            primaryKey: true
        },
        crName: {
            type: DataType.STRING,
            allowNull: false
        },
        crDateInserted: {
            type: DataType.DATE
        },
        crDateUpdated: {
            type: DataType.DATE
        },
        crIsOpen: {
            type: DataType.INTEGER,
            defaultValue: 0
        }

    })

    return ChatRoom;

}

index.js:


您必须从两个方面定义关系。用户聊天室有一个聊天室,聊天室属于用户聊天室。此外,您的关系应该放在每个模型中,而不是索引中

module.exports = function(sequelize, DataType){
    var ChatRoom = sequelize.define("ChatRoom",{
        idChatUser: {
            type: DataType.INTEGER,
            autoIncrement: true,
            primaryKey: true
        },
        //etc

    }, {
      classMethods: {
        //models/index.js calls this function
        associate: function(models) {
          ChatRoom.belongsTo(UserCharRoom, {foreignKey: 'something'});
        }
      }
    })

    return ChatRoom;

}
"use strict";

var fs        = require("fs");
var path      = require("path");
var Sequelize = require("sequelize");
var env       = process.env.NODE_ENV || "development";
var config    = require(path.join(__dirname, '..', 'config', 'config.json'))[env];

if (process.env.DATABASE_URL) {
  var sequelize = new Sequelize(process.env.DATABASE_URL,config);
} else {
  var sequelize = new Sequelize(config.database, config.username, config.password, config);
}

var db        = {};

fs
  .readdirSync(__dirname)
  .filter(function(file) {
    return (file.indexOf(".") !== 0) && (file !== "index.js")  && (file.slice(-3) === '.js');
  })
  .forEach(function(file) {
    var model = sequelize.import(path.join(__dirname, file));
    db[model.name] = model;
  });

Object.keys(db).forEach(function(modelName) {
  if ("associate" in db[modelName]) {
    db[modelName].associate(db);
  }
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

db.UserChatRoom.hasOne(db.ChatUser);
db.UserChatRoom.hasOne(db.ChatRoom, { foreignKey: 'ucrIdChatRoom' , foreignKeyConstraint:true });

db.ChatUser.belongsTo(db.UserChatRoom, { foreignKey: 'ucrIdChatRoom' , foreignKeyConstraint:true});

db.ChatRoomMessage.hasOne(db.UserChatRoom, { foreignKey: 'crmIdUserChatRoom' , foreignKeyConstraint:true });

module.exports = db;
module.exports = function(sequelize, DataType){
    var ChatRoom = sequelize.define("ChatRoom",{
        idChatUser: {
            type: DataType.INTEGER,
            autoIncrement: true,
            primaryKey: true
        },
        //etc

    }, {
      classMethods: {
        //models/index.js calls this function
        associate: function(models) {
          ChatRoom.belongsTo(UserCharRoom, {foreignKey: 'something'});
        }
      }
    })

    return ChatRoom;

}