- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的多对多表中,我有一个类型列和 Foo 模型,它在多个别名下引用 Bar。include
仅在 required
属性设置为 true
时才能正常工作。在获取 Foo
和一些关联的 Bar
时,我只收到一个子集。
我在下面添加了一个示例,可以执行该示例来演示该问题。
const { Sequelize, DataTypes } = require('sequelize')
const sequelize = new Sequelize('sqlite::memory:', {
define: { timestamps: false }
})
// defining the models to relate to each other
const Foo = sequelize.define('foo', { name: DataTypes.STRING })
const Bar = sequelize.define('bar', { status: DataTypes.STRING })
// defining the model for the relation
const FooBar = sequelize.define('fooBar', {
fooId: { type: DataTypes.STRING(32), primaryKey: true },
barId: { type: DataTypes.STRING(32), primaryKey: true },
type: { type: DataTypes.ENUM('x', 'y', 'z'), primaryKey: true }
}, { tableName: 'foo_bar' })
// defining the relationships
Foo.belongsToMany(Bar, { through: { model: FooBar, unique: false, scope: { type: 'x' } }, as: 'xs' })
Foo.belongsToMany(Bar, { through: { model: FooBar, unique: false, scope: { type: 'y' } }, as: 'ys' })
Foo.belongsToMany(Bar, { through: { model: FooBar, unique: false, scope: { type: 'z' } }, as: 'zs' });
(async () => {
await sequelize.sync()
// Instantiating the models
await Foo.bulkCreate([
{ name: 'test' },
{ name: 'empty' },
{ name: 'greenhead' }
])
await Bar.bulkCreate([
{ status: 'The Big Clash' },
{ status: 'Winter Showdown' },
{ status: 'Summer Beatdown' },
{ status: 'Test Beatdown' },
{ status: 'wrong' }
])
// Instantiating the relationships
await FooBar.bulkCreate([
{ fooId: 1, barId: 1, type: 'x' },
{ fooId: 1, barId: 1, type: 'y' },
{ fooId: 1, barId: 1, type: 'z' },
{ fooId: 1, barId: 2, type: 'y' },
{ fooId: 1, barId: 3, type: 'z' }
])
// This query outputs the correct associations but misses the Foo instances with not association to a Bar instance
const foos = await Foo.findAll({
include: [{
model: Bar,
as: 'xs',
required: true
}, {
model: Bar,
as: 'ys',
required: true
}, {
model: Bar,
as: 'zs',
required: true
}]
})
console.log(foos.map(f => f.toJSON()))
// This query outputs all Foo instances but the associations are messed up.
const foos2 = await Foo.findAll({
include: [{
model: Bar,
as: 'xs'
}, {
model: Bar,
as: 'ys'
}, {
model: Bar,
as: 'zs'
}]
})
console.log(foos2.map(f => f.toJSON()))
})()
这是带有
findAll
选项的第一个
required: true
的输出
[
{ id: 1,
name: 'test',
xs: [ { id: 1, status: 'The Big Clash', fooBar: [Object] } ],
ys:
[ { id: 1, status: 'The Big Clash', fooBar: [Object] },
{ id: 2, status: 'Winter Showdown', fooBar: [Object] } ],
zs:
[ { id: 1, status: 'The Big Clash', fooBar: [Object] },
{ id: 3, status: 'Summer Beatdown', fooBar: [Object] } ] }
]
但是没有
required: true
选项,结果是:
[ { id: 1, name: 'test', xs: [{ id: 1, status: 'The Big Clash', fooBar: [Object] }, { id: null, status: null, fooBar: [Object] } ], ys: [], zs: [] },
{ id: 2, name: 'empty', xs: [], ys: [], zs: [] },
{ id: 3, name: 'greenhead', xs: [], ys: [], zs: [] }
]
有人知道在
Bars
中包含
findAll
的正确方法吗?我想执行一个查询来获取所有 Foo 实例以及与 Bar 的关联(如果有的话)。我不明白为什么最后一次查询与 Bar 实例的关联完全搞砸了。
最佳答案
我创建了几个 hasMany 关联并再次通过 findAll 进行查询。优点:我们现在不需要在包含时使用 required: true
。
添加+更改:
// Additions
Foo.hasMany(FooBar, { as: 'fbxs', foreignKey: 'fooId', scope: { type: 'x' }, });
Foo.hasMany(FooBar, { as: 'fbys', foreignKey: 'fooId', scope: { type: 'y' }, });
Foo.hasMany(FooBar, { as: 'fbzs', foreignKey: 'fooId', scope: { type: 'z' }, });
FooBar.belongsTo(Bar, { as: 'fbb', foreignKey: 'barId' });
// Changes
const foo2 = await Foo.findAll({
// where: {
// // name: ['test', 'empty'], // if you filter by name, only matching Foos will be received
// },
include: [{
model: FooBar, as: 'fbxs',
include: [{
model: Bar, as: 'fbb'
}],
}, {
model: FooBar, as: 'fbys',
include: [{
model: Bar, as: 'fbb'
}],
}, {
model: FooBar, as: 'fbzs',
include: [{
model: Bar, as: 'fbb'
}],
}],
});
// see this original output so you have a idea
// console.log(JSON.stringify(foo2, null, 2));
// formatting as per needs
const formattedFoo = foo2.map(foo => {
const {
fbxs,
fbys,
fbzs,
...fooData
} = foo.toJSON();
// fbxs, fbys, fbzs, are in form: [ { ...FooBarData, fbb: {...BarData } } ]
// converting to: [ { ...BarData} ]
return ({
...fooData,
xs: fbxs.map(fb => fb.fbb),
ys: fbys.map(fb => fb.fbb),
zs: fbzs.map(fb => fb.fbb),
});
})
console.log(JSON.stringify(formattedFoo, null, 2));
输出:
where:{ name: ['test', 'empty'] }
- 只会返回与名称匹配的那些 Foo。
[
{
"id": 1,
"name": "test",
"xs": [ { "id": 1, "status": "The Big Clash" } ],
"ys": [ { "id": 1, "status": "The Big Clash" }, { "id": 2, "status": "Winter Showdown" } ],
"zs": [ { "id": 1, "status": "The Big Clash" }, { "id": 3, "status": "Summer Beatdown" } ]
},
{
"id": 2,
"name": "empty",
"xs": [ { "id": 1, "status": "The Big Clash" } ],
"ys": [ { "id": 2, "status": "Winter Showdown" }, { "id": 4, "status": "Test Beatdown" } ],
"zs": [ { "id": 1, "status": "The Big Clash" }, { "id": 3, "status": "Summer Beatdown" } ]
},
{
"id": 3,
"name": "greenhead",
"xs": [],
"ys": [],
"zs": []
},
{
"id": 4,
"name": "not_spock",
"xs": [],
"ys": [],
"zs": []
},
{
"id": 5,
"name": "bowl_of_petunias",
"xs": [],
"ys": [],
"zs": []
}
]
上面 findAll 生成的查询:
SELECT
`foo`.`id`,
...all columns
FROM
`foos` AS `foo`
LEFT OUTER JOIN `foo_bar` AS `fbxs` ON
`foo`.`id` = `fbxs`.`fooId`
AND `fbxs`.`TYPE` = 'x' <--------- type condition applied here after joining `foo_bar` before joining to `bars`
LEFT OUTER JOIN `bars` AS `fbxs->fbb` ON
`fbxs`.`barId` = `fbxs->fbb`.`id`
LEFT OUTER JOIN `foo_bar` AS `fbys` ON
`foo`.`id` = `fbys`.`fooId`
AND `fbys`.`TYPE` = 'y' <--------- type condition applied here after joining `foo_bar` before joining to `bars`
LEFT OUTER JOIN `bars` AS `fbys->fbb` ON
`fbys`.`barId` = `fbys->fbb`.`id`
LEFT OUTER JOIN `foo_bar` AS `fbzs` ON
`foo`.`id` = `fbzs`.`fooId`
AND `fbzs`.`TYPE` = 'z' <--------- type condition applied here after joining `foo_bar` before joining to `bars`
LEFT OUTER JOIN `bars` AS `fbzs->fbb` ON
`fbzs`.`barId` = `fbzs->fbb`.`id`;
// added extra connections
await FooBar.bulkCreate([
{ fooId: 1, barId: 1, type: 'x' },
{ fooId: 1, barId: 1, type: 'y' },
{ fooId: 1, barId: 1, type: 'z' },
{ fooId: 1, barId: 2, type: 'y' },
{ fooId: 1, barId: 3, type: 'z' },
// added extra associations
{ fooId: 2, barId: 1, type: 'x' },
{ fooId: 2, barId: 4, type: 'y' },
{ fooId: 2, barId: 1, type: 'z' },
{ fooId: 2, barId: 2, type: 'y' },
{ fooId: 2, barId: 3, type: 'z' },
]);
//....
const foo2 = await Foo.findAll({
where: {
name: ['test', 'empty'], // lets say you need 2 names
},
include: [{
model: Bar, as: 'xs',
required: true,
through: { attributes: [] }, // this removes the association(so called join table)
}, {
model: Bar, as: 'ys',
required: true,
through: { attributes: [] },
}, {
model: Bar, as: 'zs',
required: true,
through: { attributes: [] },
}],
});
console.log(JSON.stringify(foo2, null, 2));
输出
foo2
:
[
{
"id": 1,
"name": "test",
"xs": [ { "id": 1, "status": "The Big Clash" } ],
"ys": [ { "id": 1, "status": "The Big Clash" }, { "id": 2, "status": "Winter Showdown" } ],
"zs": [ { "id": 1, "status": "The Big Clash" }, { "id": 3, "status": "Summer Beatdown" } ]
},
{
"id": 2,
"name": "empty",
"xs": [ { "id": 1, "status": "The Big Clash" } ],
"ys": [ { "id": 2, "status": "Winter Showdown" }, { "id": 4, "status": "Test Beatdown" } ],
"zs": [ { "id": 1, "status": "The Big Clash" }, { "id": 3, "status": "Summer Beatdown" } ]
}
]
SELECT
`foo`.`id`,
....all columns
FROM
`foos` AS `foo`
LEFT OUTER JOIN `foo_bar` AS `xs->fooBar` ON
`foo`.`id` = `xs->fooBar`.`fooId`
LEFT OUTER JOIN `bars` AS `xs` ON
`xs`.`id` = `xs->fooBar`.`barId`
AND `xs->fooBar`.`TYPE` = 'x' <--------- type condition applied here after joining `bars`
LEFT OUTER JOIN `foo_bar` AS `ys->fooBar` ON
`foo`.`id` = `ys->fooBar`.`fooId`
LEFT OUTER JOIN `bars` AS `ys` ON
`ys`.`id` = `ys->fooBar`.`barId`
AND `ys->fooBar`.`TYPE` = 'y' <--------- type condition applied here after joining `bars`
LEFT OUTER JOIN `foo_bar` AS `zs->fooBar` ON
`foo`.`id` = `zs->fooBar`.`fooId`
LEFT OUTER JOIN `bars` AS `zs` ON
`zs`.`id` = `zs->fooBar`.`barId`
AND `zs->fooBar`.`TYPE` = 'z'; <--------- type condition applied here after joining `bars`
关于sequelize.js - Sequelize include 不能在多对多与带有类型列的槽表上正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66349616/
这个问题已经有答案了: 已关闭12 年前。 Possible Duplicates: what is the difference between #include and #include “fi
我想使用 #include 指令,其文件名作为外部定义的宏传递。 例如 #include #FILE".h" 其中 FILE 将被定义为字符串 MyFile(不带引号),结果为 #include "M
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 7年前关闭。 Improve thi
我想在当前目录及其子目录下的每个 .m 文件中查找所有出现 ncread 的情况。我使用以下命令: grep -R --include="\.m" ncread . 但是该命令没有返回任何内容。 gr
有时我会遇到这样的情况,我发现我需要为大型第三方文件制作一个#include,这样我才能使用一个函数或一个小类,这让我感到内疚,因为我知道这已经消失了增加我的编译时间,因为当我只想要一个功能时它会编译
这个问题在这里已经有了答案: 关闭13年前. Possible Duplicate: what is the difference between #include and #include “fi
我正在尝试通过应用程序加载器提交应用程序。我收到这个错误。但我已经检查了build设置,所有三种架构都包含在有效架构设置中。 最佳答案 断开任何设备,只保留“iOS 设备”中的选项并将其存档。 关于i
Please check this demo plunker更好地理解我的问题。 在我的主页上有一个表格。每个表行后面都有一个最初隐藏的空行。单击第一行时,我使用指令在其下方的空行中注入(inject
我正在使用 mkdocs 创建 html 网页和片段扩展以将我的主文档分成小块。我有一个难以理解的错误: 在我制作的文件file1.md中: --8<-- includes/some_rep/frag
include的推荐方式是什么?您项目的所有文件? 我见过很多使用类似结构的例子: include 的有序列表单个顶级文件(定义 Module 的文件,或应用程序中的“主”文件)中的语句。 这似乎也是
我想知道如何使用 fx:include与 JavaFX Scene Builder 结合使用,因此: 想象我有一个 BorderPane (文件 borderpane.fxml)。在中间部分我想放一个
我看到 Fortran 有“调用”和“包含”语句。两者有什么区别? .i 文件类型有什么意义吗? 即: include 'somefile.i' call 'somesubroutine.f' 谢谢!
这很挑剔,可能没有任何实际用途。我只是好奇... 在 C++20 工作草案 (n4861) 中, header 名称定义为: (5.8) header-name: " q-char-
这个问题已经有答案了: 已关闭10 年前。 Possible Duplicate: What is the difference between #include and #include “fil
我有一个非常庞大且臃肿的类,我想将它拆分成单独的文件,但它应该对用户完全透明并且与使用该类的现有项目兼容。 特别是,我有自己的 ImageMatrix 类,它定义了大量的一元函数、大量带有标量的二元函
我是 grep 的新手,在重构 C 和 C++ 文件的过程中,我遇到了替换系统的问题,包括 #include <>与本地包括 #include "" . 有没有一种方法可以将 grep 与任何替代工具
我正在制作一个 Spring MVC web 项目,我必须有一个常量 header 。 我的基本要求是“我们希望在所有屏幕上都有一个标题,以显示谁登录了 ProjectA。” 我从这里“What is
在 SWIG 中,“%include”指令与标准 C“#include”有什么区别? 例如,在所有教程中,为什么它们通常看起来像这样: %module my_module %{ #include "M
假设我们有这个头文件: MyClass.hpp #pragma once #include class MyClass { public: MyClass(double); /* .
我已经在一个项目上工作了一段时间,该项目实现了一个使用 C 库的自定义框架。该框架是用 Swift 编写的,我创建了一个模块来向 Swift 公开 C 头文件。该框架是在不同的项目中启动的,然后将该框
我是一名优秀的程序员,十分优秀!