Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.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

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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.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
Node.js 似乎无法在sequelize节点中为同一模型设置多个外键引用_Node.js_Postgresql_Sequelize.js - Fatal编程技术网

Node.js 似乎无法在sequelize节点中为同一模型设置多个外键引用

Node.js 似乎无法在sequelize节点中为同一模型设置多个外键引用,node.js,postgresql,sequelize.js,Node.js,Postgresql,Sequelize.js,我有一个我正在构建的节点后端,我有两个使用sequelize构建的模型。用户模型和朋友模型。我想在朋友模型中有两个字段(requester,requested),它们引用用户模型中的两个不同用户 我现在在postgres shell中设置它的方式是,只有一个对用户模型的引用是firends_userId。我想把它改成两个 这是我的密码: 型号: const seq = require('sequelize'); const { postgres } = require('../../index'

我有一个我正在构建的节点后端,我有两个使用sequelize构建的模型。用户模型和朋友模型。我想在朋友模型中有两个字段(requester,requested),它们引用用户模型中的两个不同用户

我现在在postgres shell中设置它的方式是,只有一个对用户模型的引用是firends_userId。我想把它改成两个

这是我的密码:

型号:

const seq = require('sequelize');
const { postgres } = require('../../index');
const { user } = require('./User')

const friend = postgres.define(
  "friend",
  {
    id: {
      type: seq.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    blocked: {
      type: seq.BOOLEAN,
      default: false,
      null: false
    },
    favorite: {
      type: seq.BOOLEAN,
      default: false,
      null: false
    },
  },{
    createdAt:  seq.DATE,
    updatedAt:  seq.DATE,
  },
);
postgres.sync()
  .then(() => {
    console.log("friend table is synced")
  })
  .catch((error) => {
    console.log("caught error with friend:  " + error)
  })

user.hasMany(friend, {as: 'friender', foreignkey: 'frienderId'});
user.hasMany(friend, {as: 'friended', foreignkey: 'friendedId'});

module.exports.friend = friend;

这是postgres shell中的当前表

splitter=# \d+ friends;
                                                         Table "public.friends"
  Column  |           Type           | Collation | Nullable |               Default               | Storage | Stats target | Description 
----------+--------------------------+-----------+----------+-------------------------------------+---------+--------------+-------------
 id       | integer                  |           | not null | nextval('friends_id_seq'::regclass) | plain   |              | 
 blocked  | boolean                  |           |          |                                     | plain   |              | 
 favorite | boolean                  |           |          |                                     | plain   |              | 
 DATE     | timestamp with time zone |           | not null |                                     | plain   |              | 
 userId   | integer                  |           |          |                                     | plain   |              | 
Indexes:
    "friends_pkey" PRIMARY KEY, btree (id)
Foreign-key constraints:
    "friends_userId_fkey" FOREIGN KEY ("userId") REFERENCES users(id) ON UPDATE CASCADE ON DELETE SET NULL

splitter=# 

通过为多个列指定
primaryKey:true
,可以在Sequelize模型中编写复合主键,但Sequelize当前不支持复合主键,因此无法执行此操作

请参阅(与组合外键相关的讨论,在压缩中不可能这样做,这直接意味着组合主键也不可能)

对于具有指向一个表的多个外键,可以使用方法

有关更多详细信息,请参阅:

文件:


通过为多个列指定
primaryKey:true
,可以在Sequelize模型中编写复合主键,但Sequelize当前不支持复合主键,因此无法执行此操作

请参阅(与组合外键相关的讨论,在压缩中不可能这样做,这直接意味着组合主键也不可能)

对于具有指向一个表的多个外键,可以使用方法

有关更多详细信息,请参阅:

文件:


我的意思是说外键不是主键更新了答案,也提供了一个参考。希望能有所帮助。在我看过你链接的文章后,它起了作用。thanksI的意思是说外键不是主键更新了答案,也提供了一个参考。希望能有所帮助。在我看了你链接的文章后,它起了作用。谢谢
splitter=# \d+ friends;
                                                         Table "public.friends"
  Column  |           Type           | Collation | Nullable |               Default               | Storage | Stats target | Description 
----------+--------------------------+-----------+----------+-------------------------------------+---------+--------------+-------------
 id       | integer                  |           | not null | nextval('friends_id_seq'::regclass) | plain   |              | 
 blocked  | boolean                  |           |          |                                     | plain   |              | 
 favorite | boolean                  |           |          |                                     | plain   |              | 
 DATE     | timestamp with time zone |           | not null |                                     | plain   |              | 
 userId   | integer                  |           |          |                                     | plain   |              | 
Indexes:
    "friends_pkey" PRIMARY KEY, btree (id)
Foreign-key constraints:
    "friends_userId_fkey" FOREIGN KEY ("userId") REFERENCES users(id) ON UPDATE CASCADE ON DELETE SET NULL

splitter=#