当我查询具有 M:N 关系的模型并在其中嵌套更多包含内容时 - 我将对象键名称截断为固定长度。
UserGroup 与 BitrixResponsible 具有 M:N 关系,即“bitrix_responsible” 有一个名为 BitrixPortal 的模型,其中有一列名为“webhook_inbound_url”
使用这样的模型查询参数:
{
include: [
{model: models.BitrixResponsible.scope('with_relations_deep'), as: 'bitrix_responsible'}
]
}
和配置范围
BitrixAccount.addScope('with_relations', {
include: [
{ model: BitrixPortal.scope('self_only'), as: 'bitrix_portal'},
],
})
BitrixResponsible.addScope('with_relations_deep', {
include: [
{ model: BitrixAccount.scope('with_relations'), as: 'bitrix_account'},
{ model: UserPosition, as: 'position'},
],
})
和 BitrixPortal 模型
class BitrixPortal extends Model {}
BitrixPortal.init(
{
id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, allowNull: false, primaryKey: true },
account_id: { type: DataTypes.UUID, allowNull: false },
default_responsible_id: { type: DataTypes.UUID, allowNull: true },
// other fields
webhook_inbound_url: { type: DataTypes.STRING, allowNull: true },
webhook_outbound_token: { type: DataTypes.STRING, allowNull: true },
// other fields
},
{
...default_model_options(
'bitrix_portals',
'bitrix',
'BitrixPortal',
true, true),
defaultScope: {
attributes: {
exclude: ['webhook_inbound_url', 'webhook_outbound_token']
},
include: [],
},
scopes: {
...default_model_scopes(),
},
}
)
我查询了UserGroup的一条记录,而不是通过路径查询某个值
record -> bitrix_responsible -> [0] -> bitrix_account -> bitrix_portal -> webhook_inbound_url
我得到了一条带有键“webhook_inboun”的记录
data.bitrix_responsible[0].bitrix_account.bitrix_portal.webhook_inboun
在生成的嵌套 BitrixPortal 记录中还有其他具有长名称的键,这些名称也被删除。
查询响应示例
{
"created_by": "023b123d-f535-4b8b-9964-e60f12279ea6",
"updated_by": "023b123d-f535-4b8b-9964-e60f12279ea6",
"deleted_by": null,
"created_at": "2023-09-27T08:22:12.394Z",
"updated_at": "2023-09-28T03:22:12.787Z",
"deleted_at": null,
"hierarchyLevel": 2,
"parentId": "11625860-5095-433a-a1b8-72862bf10430",
"users": [...],
"managers": [...],
"access_assigns": [...],
"telegram_auth_requests": [...],
"ticket_responsibles": [],
"bitrix_responsibles": [
{
"bitrix_account": {
"bitrix_portal": {
"base_url": null,
"data": {},
"id": "a402473f-4286-4431-8ce8-c72d345a6071",
"account_id": "ceb07684-9abe-43c0-a262-5923d4427d9a",
"default_respon": "cc8e44b1-0578-4e17-8a9f-738d52a9b132", // <- HERE
"b24_user_id": "1061",
"b24_group_id": null,
"b24_crm_prefix": "T95",
"b24_crm_id": null,
"webhook_inboun": "https://domain.bitrix24.ru/rest/1265/token_asdasd/", // <- HERE
"webhook_outbou": null, // <- HERE
"is_active": true,
"created_by": "023b123d-f535-4b8b-9964-e60f12279ea6",
"updated_by": "71ea0ad5-cee5-4629-afa9-247d04412907",
"deleted_by": null,
"created_at": "2023-09-27T05:04:17.347Z",
"updated_at": "2023-09-27T09:46:39.899Z",
"deleted_at": null
}
},
"position": {
"id": "48a6bc8a-f84b-4501-963e-ff366868d9b1",
}
}
],
}
用相同的包含替换范围给了我相同的结果,所以这不是“范围”相关的问题。
目前我查询 BitrixResponsible 而不使用嵌套包含。
{
include: [
{model: models.BitrixResponsible.scope('self_only'), as: 'bitrix_responsible'}
]
}
收集 record.bitrix_responsible 的 ID 并查询范围为
'with_relations_deep'
和 where: { id: record.bitrix_responsible.map(r => r.id) }
的 BitrixResponsible
而且它工作正常,没有键名被删除。
版本:
所以我的问题是:这是一个错误还是我缺少一些续集选项/版本更新来防止这种行为?
PostgreSQL 默认情况下对列名和别名有 64 个字符的限制,因此您应该使用 Sequelize 实例的
minifyAliases
选项:
定义是否应缩小别名的标志(主要用于避免 Postgres 别名字符限制为 64)