Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.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
Javascript 在Sequelize中关联未按预期工作_Javascript_Node.js_Postgresql_Express_Sequelize.js - Fatal编程技术网

Javascript 在Sequelize中关联未按预期工作

Javascript 在Sequelize中关联未按预期工作,javascript,node.js,postgresql,express,sequelize.js,Javascript,Node.js,Postgresql,Express,Sequelize.js,我试图在Sequelize中关联两个表,但是我得到了SequelizeAgerLoadingError,即一个表与另一个表不关联,尽管在这个平台上尝试了所有可用的修复程序 我有两个表,User和Item 用户(User.js) Item(Item.js) 如上图所示,用户拥有多个(项目),而项目则属于(用户) 但是,当我对Item表进行查询时(如下所示) 我得到SequelizeAgerLoadingError,即“用户与项目不关联!” 我已经尝试了所有可用的修复方法,包括和,但没有成功。我终于

我试图在Sequelize中关联两个表,但是我得到了SequelizeAgerLoadingError,即一个表与另一个表不关联,尽管在这个平台上尝试了所有可用的修复程序

我有两个表,User和Item

用户(User.js)

Item(Item.js)

如上图所示,用户拥有多个(项目),而项目则属于(用户)

但是,当我对Item表进行查询时(如下所示)

我得到SequelizeAgerLoadingError,即“用户与项目不关联!”


我已经尝试了所有可用的修复方法,包括和,但没有成功。

我终于找到了解决方法。首先,我删除了表并放弃了模型定义。其次,我使用
sequelize model:create--name ModelName--attributes columnName:columnType
命令生成了迁移和模型。然后,我使用生成的模型将这两个表关联起来,就像我之前所做的那样。最后,我运行了
sequelize db:migrate
命令来创建表,在运行查询时,它成功了

之前,我是手动创建模型的。加载模型后,我还使用
sequelize.sync({force:false/true})
命令创建表

用户模型(User.js)

项目模型(Item.js)

查询(queryitem.js)


我终于找到了解决办法。首先,我删除了表并放弃了模型定义。其次,我使用
sequelize model:create--name ModelName--attributes columnName:columnType
命令生成了迁移和模型。然后,我使用生成的模型将这两个表关联起来,就像我之前所做的那样。最后,我运行了
sequelize db:migrate
命令来创建表,在运行查询时,它成功了

之前,我是手动创建模型的。加载模型后,我还使用
sequelize.sync({force:false/true})
命令创建表

用户模型(User.js)

项目模型(Item.js)

查询(queryitem.js)


您应该使用在项目模型中为用户定义的别名,而且我认为您应该在用户模型中使用别名项目而不是用户,这对我来说更有意义,可能问题在于别名中的双重性。最后一件事是你应该使用一次同步方法,毕竟不是在所有的模型中,这不是一个好方法。@RichardSolár我已经纠正了别名,但仍然不起作用。你能在一些示例代码中重现它并共享一个repo吗?我终于找到了一个解决方法。首先,我把桌子扔了。其次,我使用“sequelize model:create--name ModelName--attributes columnName:columnType”命令生成迁移和模型。然后,我使用生成的模型将这两个表关联起来,就像我之前所做的那样。最后,我运行了“sequelize db:migrate”命令来创建表,在运行查询时,它成功了!所以,模型中的同步方法似乎是个问题。您应该使用在项目模型中为用户定义的别名,而且我认为您应该在用户模型中使用别名项目而不是用户,这对我来说更有意义,可能问题在于别名中的重复性。最后一件事是你应该使用一次同步方法,毕竟不是在所有的模型中,这不是一个好方法。@RichardSolár我已经纠正了别名,但仍然不起作用。你能在一些示例代码中重现它并共享一个repo吗?我终于找到了一个解决方法。首先,我把桌子扔了。其次,我使用“sequelize model:create--name ModelName--attributes columnName:columnType”命令生成迁移和模型。然后,我使用生成的模型将这两个表关联起来,就像我之前所做的那样。最后,我运行了“sequelize db:migrate”命令来创建表,在运行查询时,它成功了!所以看起来模型中的同步方法是个问题
const User = dbconnection.sequelize.define('users', {
    id:  { type: Sequelize.INTEGER, autoIncrement: true, primaryKey: true},
    name: {
        type:  Sequelize.STRING(80),
        allowNull: false
    },
    email: {
        type:  Sequelize.STRING(120),
        allowNull: false,
        unique: true
    },
    dob: {
        type: Sequelize.DATEONLY,
        allowNull: false
    },
    password: {
        type:  Sequelize.STRING(256),
        allowNull: false
    }

});

User.associate = models => {
    User.hasMany(models.Item, { as: 'items',foreignKey: 'user_id' })
}

dbconnection.sequelize.sync({ force: false })
    .then(() => {
        //console.log('Table created!')
    });

module.exports = {
    User
};

const Item = dbconnection.sequelize.define('items', {
    id:  { type: Sequelize.INTEGER, unique: true, autoIncrement: true, primaryKey: true},
    item: {
        type: Sequelize.STRING(80),
        allowNull: true
    },
    item_type: {
        type: Sequelize.STRING(10),
        allowNull: false
    },
    comment: {
        type: Sequelize.STRING(1000),
        allowNull: true
    },
    user_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: { model: 'users', key: 'id' }
    },
});

Item.associate = models => {
    Item.belongsTo(models.User, { as: 'users',foreignKey: 'user_id' })
}

dbconnection.sequelize.sync({ force: false })
    .then(() => {
        // console.log('Table created!')
    })
});

module.exports = {
    Item
};

const usersdb = require('./userdb')
const itemsdb = require('./itemdb')

class ItemsController {
    static async getAllItems(req, res, next) {
        try{
            let allitems = await itemsdb.Item.findAll({
                include: [{
                    model: usersdb.User
                }]
            })
            return {items: allitems, status: true}
        }
        catch (e) {
            return {items: e, status: false}
        }
    }
}

module.exports = ItemsController;

'use strict';
module.exports = (sequelize, DataTypes) => {
  const User = sequelize.define('User', {
    email: {
        type:  DataTypes(120),
        allowNull: false,
        unique: true
    },
    dob: {
        type: DataTypes.DATEONLY,
        allowNull: false
    },
    password: {
        type:  DataTypes.STRING(256),
        allowNull: false
    }
  }, {});
  User.associate = function(models) {
    User.hasMany(models.Item, {as: 'Item', foreignKey: 'user_id'})
  };
  return User;
};

'use strict';
module.exports = (sequelize, DataTypes) => {
  const Item = sequelize.define('Item', {
    item: {
      type: DataTypes.STRING(80),
      allowNull: true
    },
    item_type: {
      type: DataTypes.STRING(10),
      allowNull: false
    },
    comment: {
      type: DataTypes.STRING(1000),
      allowNull: true
    },
    user_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: { model: 'User', key: 'id' }
    }
  }, {});
  Item.associate = function(models) {
    Item.belongsTo(models.User, { as: 'User',foreignKey: 'user_id' })
  };
  return Item;
};

const Item = require('../models').Item
const User = require('../models').User

class ItemsController {
    static async getAllItems() {
        try{
            let allitems = await Item.findAll({
                include: [{
                    model: User,
                    as: 'User'
                }]
            })
            return {items: allitems, status: true}
        }
        catch (e) {
            return {items: e, status: false}
        }
    }
}

module.exports = ItemsController;