续集多对多的自我参考

时间:2015-02-06 17:55:53

标签: postgresql sequelize.js

我正试图让sequelize中的一个关联examples正常工作,而且似乎没有正确设置连接表。在这个例子中,我们有一个名为Person的模型,然后是一个Person的孩子的多对多自引用。代码:

var Sequelize = require('sequelize');
var sequelize = new Sequelize('postgres://root@localhost/database_bug');

var Person = sequelize.define('Person', {
  id: {
    type: Sequelize.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  name: {
    type: Sequelize.STRING,
    allowNull: false
  }
});

Person.belongsToMany(Person, { as: 'Children', through: 'PersonChildren' });

Person.sequelize.sync({force:true}).then(function() {
  Person.build({ name: 'John Doe' }).save().then(function(father) {
    Person.build({ name: 'Jane Doe' }).save().then(function(daughter) {
      father.addChild(daughter);
    });
  });
});

但是当我在postgres中查看我的表时,我觉得自动生成的连接表中缺少一列。

            List of relations
 Schema |      Name      |   Type   | Owner 
--------+----------------+----------+-------
 public | People         | table    | root
 public | People_id_seq  | sequence | root
 public | PersonChildren | table    | root

                                    Table "public.People"
  Column   |           Type           |                       Modifiers                       
-----------+--------------------------+-------------------------------------------------------
 id        | integer                  | not null default nextval('"People_id_seq"'::regclass)
 name      | character varying(255)   | not null
 createdAt | timestamp with time zone | not null
 updatedAt | timestamp with time zone | not null
Indexes:
    "People_pkey" PRIMARY KEY, btree (id)
Referenced by:
    TABLE ""PersonChildren"" CONSTRAINT "PersonChildren_PersonId_fkey" FOREIGN KEY ("PersonId") REFERENCES "People"(id)

          Table "public.PersonChildren"
  Column   |           Type           | Modifiers 
-----------+--------------------------+-----------
 createdAt | timestamp with time zone | not null
 updatedAt | timestamp with time zone | not null
 PersonId  | integer                  | not null
Indexes:
    "PersonChildren_pkey" PRIMARY KEY, btree ("PersonId")
Foreign-key constraints:
    "PersonChildren_PersonId_fkey" FOREIGN KEY ("PersonId") REFERENCES "People"(id)

PersonChildren需要一个名为ChildId的列或沿着这些行的内容将Person链接到其子级。

人员表:

database_bug=# SELECT * FROM "People";
 id |   name   |         createdAt          |         updatedAt          
----+----------+----------------------------+----------------------------
  1 | John Doe | 2015-02-06 09:36:44.975-08 | 2015-02-06 09:36:44.975-08
  2 | Jane Doe | 2015-02-06 09:36:44.985-08 | 2015-02-06 09:36:44.985-08

Weirder仍然,我选择确保将daughter作为孩子添加到father

database_bug=# SELECT * from "PersonChildren";
         createdAt          |         updatedAt          | PersonId 
----------------------------+----------------------------+----------
 2015-02-06 09:36:44.997-08 | 2015-02-06 09:36:44.997-08 |        2

但是PersonId是2而不是1. father应该添加daughter,而不是相反。

如何让这种关联有效?

1 个答案:

答案 0 :(得分:0)

好的,看起来文档中的示例是错误的。为了公平起见,他们确实说必须使用hasMany,然后使用belongsToMany显示示例。

我将belongsToMany更改为hasMany,看起来我们很高兴:

          Table "public.PersonChildren"
  Column   |           Type           | Modifiers 
-----------+--------------------------+-----------
 createdAt | timestamp with time zone | not null
 updatedAt | timestamp with time zone | not null
 PersonId  | integer                  | not null
 ChildId   | integer                  | not null

database_bug=# select * from "PersonChildren";
         createdAt          |         updatedAt          | PersonId | ChildId 
----------------------------+----------------------------+----------+---------
 2015-02-06 10:04:21.624-08 | 2015-02-06 10:04:21.624-08 |        1 |       2

现在我可以做father.getChildren(),并且promise将返回一个子列表。