gpt4 book ai didi

postgresql - Sequelize 多对多自引用

转载 作者:行者123 更新时间:2023-12-03 22:42:49 27 4
gpt4 key购买 nike

我正在尝试使 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 ,而不是相反。

任何想法如何让这个协会工作?

最佳答案

好的,看起来文档中的示例是错误的。公平地说,他们确实说过必须使用 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 将返回一个 child 的列表。

关于postgresql - Sequelize 多对多自引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28371847/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com