这是相关的Apollo代码。 (省略了许多数据库字段,使其更快/更容易阅读这篇文章。)
连接器
const userAccountsDataModel = db.define('userAccountsData', {
id: {type: Sequelize.STRING, primaryKey: true},
picture_medium: {type: Sequelize.STRING}
});
const associatesDataModel = db.define('associatesData', {
_id: {type: Sequelize.STRING, primaryKey: true},
first_name: {type: Sequelize.STRING},
last_name: {type: Sequelize.STRING},
userID: {type: Sequelize.STRING},
});
associatesDataModel.belongsTo(userAccountsDataModel, {foreignKey: 'userID'});
SCHEMA
type userAccountsData {
id: String
picture_medium: String
}
type associatesData {
_id: String
first_name: String
last_name: String
userID: String
userAccountsData: [userAccountsData]
}
QUERY
const GETONEASSOCIATE_QUERY = gql`
query getOneAssociate($_id: String!) {
getOneAssociate(_id: $_id) {
_id
first_name
last_name
userID
accountsData{
id
picture_medium
}
}
} `;
RESOLVER
getOneAssociate(_, args) {
console.log('in getOneAssociate');
var ret = connectors.associatesData.findAll({
where: args,
include: [connectors.userAccountsData],
logging: console.log
}).then((res) => res.map((item) => item.dataValues));
return ret;
},
通过Sequelize,解析器生成以下SQL:
SELECT "associatesData"."_id",
"associatesData"."first_name",
"associatesData"."last_name",
"associatesData"."userID",
"userAccountsDatum"."id" AS "userAccountsDatum.id",
"userAccountsDatum"."picture_medium" AS "userAccountsDatum.picture_medium"
FROM "associatesDatas" AS "associatesData"
LEFT OUTER JOIN "userAccountsData" AS "userAccountsDatum"
ON "associatesData"."userID" = "userAccountsDatum"."id"
WHERE "associatesData"."_id" = '35';
当我在SQL客户端中运行上述SQL时,我可以看到所有要搜索的数据都被返回,包括来自相关“userAccountsData”表的数据。
但是,当数据到达我在javascript浏览器中运行的客户端代码时,相关表中的数据将丢失 - 应该包含它的对象为null。
我错过了什么?
提前致谢所有信息!
答案 0 :(得分:0)
Sequize自动复制表名,结果证明这是一个异常现象。
默认情况下,Sequelize会自动复数表名。应该可以通过使用Sequelize的freezeTableName
属性来关闭此行为。但是,根据this Sequelize issue post,有些情况下,Sequelize会忽略freezeTableName
属性并且无论如何都会复数(有时会单一化)表名。
在发现这个之后,我能够通过复制相关的表名并通过我的所有Apollo代码来更新对该表的引用来解决异常,以便它们也是多元化的。