Sequelize many-to-many 自引用
Sequelize many-to-many self reference
我正在尝试从 sequelize 中获得关联 examples 之一,但它似乎没有正确设置连接 table。在示例中,我们有一个名为 Person 的模型,然后有一个 many-to-many self-reference 表示一个人的 children。代码:
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 中查看我的 tables 时,我觉得 auto-generated 连接 table.
中缺少一列
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
的专栏或类似的内容以 link 一个人到其 Child.
人数table:
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
更奇怪的是,我 select 确保 daughter
作为 child 添加到 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
,而不是相反。
有什么想法可以让这个协会发挥作用吗?
好的,看起来文档中的示例是错误的。公平地说,他们确实说必须使用 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() 并且承诺将 return 子列表。
我正在尝试从 sequelize 中获得关联 examples 之一,但它似乎没有正确设置连接 table。在示例中,我们有一个名为 Person 的模型,然后有一个 many-to-many self-reference 表示一个人的 children。代码:
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 中查看我的 tables 时,我觉得 auto-generated 连接 table.
中缺少一列 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
的专栏或类似的内容以 link 一个人到其 Child.
人数table:
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
更奇怪的是,我 select 确保 daughter
作为 child 添加到 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
,而不是相反。
有什么想法可以让这个协会发挥作用吗?
好的,看起来文档中的示例是错误的。公平地说,他们确实说必须使用 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() 并且承诺将 return 子列表。