Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.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
Postgresql Sails js和Sequelize,未向数据库添加关联_Postgresql_Associations_Sails.js_Sequelize.js - Fatal编程技术网

Postgresql Sails js和Sequelize,未向数据库添加关联

Postgresql Sails js和Sequelize,未向数据库添加关联,postgresql,associations,sails.js,sequelize.js,Postgresql,Associations,Sails.js,Sequelize.js,以下是我的模型在sails中的结构: myapp --api ----controllers ----models -----User.js ------Role.js User.js module.exports = { attributes:{ id: { type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true }, username: { type:

以下是我的模型在sails中的结构:

myapp
--api
----controllers
----models
-----User.js
------Role.js
User.js

module.exports = {
 attributes:{
   id: {
      type: Sequelize.INTEGER,
      primaryKey: true,
      autoIncrement: true
   },
   username: {
       type: Sequelize.STRING,
   },
   password: {
       type: Sequelize.STRING,
   }
 },
 associations: function() {
   User.hasOne(Role, {foreignKey: 'id', as: 'role' });
 }
};
Role.js

module.exports = {
 attributes:{
   id: {
      type: Sequelize.INTEGER,
      primaryKey: true,
      autoIncrement: true
   },
   name: {
       type: Sequelize.STRING,
   }
 }
};
升空后,在postgresql中,我有id为、用户名为、密码为、createdat和updatedat的users表,以及id为、名称为、createdat和updatedat的roles表。用户表中的角色没有foreignKey

我怎样才能解决这个问题? 我使用的是sails hook sequelize和sails hook sequelize蓝图,这会因为它们而发生吗

谢谢

编辑: 正确的方法是:

module.exports = {
 attributes:{
   id: {
      type: Sequelize.INTEGER,
      primaryKey: true,
      autoIncrement: true
   },
   username: {
       type: Sequelize.STRING,
   },
   password: {
       type: Sequelize.STRING,
   }
 },
 associations: function() {
     User.hasOne(Role, {
      as : 'role',
      foreignKey: {
        name: 'roleId',
        allowNull: false
      }
    });
 }
};

默认情况下会添加
createdAt
updatedAt
列,除非将
timestamps
选项设置为false。看

要添加外键约束,需要为角色模型定义