Sequelize 多对多自引用
Posted
技术标签:
【中文标题】Sequelize 多对多自引用【英文标题】:Sequelize many-to-many self reference 【发布时间】:2015-04-06 22:53:48 【问题描述】:我正在尝试从 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 链接到它的 Child。
人员表:
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
更奇怪的是,我选择确保将 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】:好的,看起来文档中的示例是错误的。公平地说,他们确实说必须使用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 将返回一个孩子列表。
【讨论】:
此问题已在 Sequelize 中修复。他们现在建议您在创建多对多自引用时使用.belongsToMany
。
在创建多对多自引用时,有没有 belongsToMany 的例子?以上是关于Sequelize 多对多自引用的主要内容,如果未能解决你的问题,请参考以下文章