我错过了一些东西,它把我逼疯了。
这是一个非常简单的使用withGraphFetched加载的查询,我可以看到相关的查询在我的控制台中正确地执行,甚至在我的SQL编辑器中测试了查询及其正确,但是反对实际上并不是将结果分配给对象,我也不知道为什么。
在下面的示例中,响应对象上的media键始终是空数组。即使查询实际上返回记录。
奇怪的是,如果我从使用withGraphFetched改为withGraphJoined,它将返回正确的数据,尽管只有一条记录。
export default class Article extends Model implements IArticle {
static tableName = TABLES.ARTICLES;
static get relationMappings() {
return {
media: {
relation: Model.HasManyRelation,
modelClass: Media,
join: {
from: "articles.id",
to: "media.article_id",
},
}
}
}export default class Media extends Model implements IMedia {
static tableName = TABLES.MEDIA;
static get relationMappings() {
return {
article: {
relation: Model.BelongsToOneRelation,
modelClass: Article,
join: {
from: "media.article_id",
to: "articles.id",
},
},
};
}
}和查询
Article.query().withGraphFetched("media");返回(注意空的media[])
{
"id": 1,
"slug": "nsw-flooding",
"title": "NSW Flooding",
"description": "Thousands evacuated from low lying areas across Sydney and NSW, with warnings of flood peaks not seen since 1922.",
"media": []
},我可以清楚地看到我的控制台
knex:query select "media".* from "media" where "media"."article_id" in (?, ?, ?, ?, ?, ?, ?, ?, ?) undefined当直接在数据库上运行时,它会显示正确的记录.
奇怪的是,当切换到
Article.query().withGraphJoined("media");虽然只显示了一条记录,但它还是有效的。
{
"id": 1,
"slug": "nsw-flooding",
"title": "NSW Flooding",
"description": "Thousands evacuated from low lying areas across Sydney and NSW, with warnings of flood peaks not seen since 1922.",
"media": [
{
"id": 18,
"assetId": "xxxx"
}
]
},因此,在反对级别上发生了一些事情,它没有附加记录,而且由于某种原因,withGraphFetched的行为方式很奇怪,我不理解。
任何帮助都将不胜感激!
发布于 2021-04-22 22:33:09
如果其他人遇到这个令人困惑的问题,这个问题就由项目的一个维护人员帮我解决了。答案
You are using knexSnakeCaseMappers, but not declaring things in camel case发布于 2021-12-12 15:47:22
我有一个与问题相反的问题,即withGraphFetched所有的东西都运行得很好,但是当更改为withGraphJoined时,所有的关系都是空对象。我终于找到了这个解释,因为在我发现之前,它让我发疯了,所以我把它发到这里,以防有人用同样的问题降落在这里。
我遇到的问题是由仍然打开的[医]克氏虫引起的,如果使用connectionString/connectString和Postgres,会使columnInfo失败。下面是我的问题配置,然后是固定配置。
// withGraphJoined doesn't work
const knex = Knex({
client: "pg",
connection: {
connectionString: process.env.DATABASE_URL,
},
});
// withGraphJoined works
const knex = Knex({
client: "pg",
connection: process.env.DATABASE_URL,
});如果您有连接属性,那么一个选项是使用pg-connection-string包解析连接字符串,例如:
const { parse } = require("pg-connection-string");
const knex = Knex({
client: "pg",
connection: {
...parse(process.env.DATABASE_URL),
ssl: { rejectUnauthorized: false },
},
});https://stackoverflow.com/questions/67209589
复制相似问题