gpt4 book ai didi

mysql - Feathers sequelize 按关联列查找

转载 作者:行者123 更新时间:2023-12-03 22:30:45 25 4
gpt4 key购买 nike

我正在尝试根据关联表的值过滤数据,当我查找全部时它工作正常但是当我尝试按名称搜索时,我得到一个列不存在错误
我已经设置了一个钩子(Hook),如 https://github.com/feathersjs-ecosystem/feathers-sequelize#associations 中所述
在 postman 中,我可以将关联视为子键,但我无法按任何列搜索
获取代码

service.find({
query: {
include: ["policyholders"],
Name: {
$like: `%somename%`,
},
},
});
政策模型
// See http://docs.sequelizejs.com/en/latest/docs/models-definition/
// for more of what you can do here.
const Sequelize = require("sequelize");
const DataTypes = Sequelize.DataTypes;

module.exports = function (app) {
const sequelizeClient = app.get("sequelizeClient");
const policies = sequelizeClient.define(
"policies",
{
PolicyId: {
autoIncrement: true,
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
},
PolicyNumber: {
type: DataTypes.STRING(30),
allowNull: true,
},
PolicyHolderId: {
type: DataTypes.INTEGER,
allowNull: true
},
},
{
hooks: {
beforeCount(options) {
options.raw = true;
},
},
}
);

// eslint-disable-next-line no-unused-vars
policies.associate = function (models) {
// Define associations here
// See http://docs.sequelizejs.com/en/latest/docs/associations/
const { policyholders } = models;
policies.belongsTo(policyholders, { foreignKey: "PolicyholderId" });
};

return policies;
};
投保人模式
// See http://docs.sequelizejs.com/en/latest/docs/models-definition/
// for more of what you can do here.
const Sequelize = require('sequelize');
const DataTypes = Sequelize.DataTypes;

module.exports = function (app) {
const sequelizeClient = app.get('sequelizeClient');
const policyholders = sequelizeClient.define(
'policyholders',
{
PolicyholderId: {
autoIncrement: true,
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
},
Name: {
type: DataTypes.STRING(250),
allowNull: true,
},
},
{
hooks: {
beforeCount(options) {
options.raw = true;
},
},
}
);

// eslint-disable-next-line no-unused-vars
policyholders.associate = function (models) {
// Define associations here
// See http://docs.sequelizejs.com/en/latest/docs/associations/
const { policies } = models;

policyholders.hasMany(policies, { foreignKey: { name: 'PolicyholderId' } });
};

return policyholders;
};
政策 Hook
const { authenticate } = require('@feathersjs/authentication').hooks;

const getRelated = require('../../hooks/get-related');

module.exports = {
before: {
all: [ authenticate('jwt') ],
find: [getRelated()],
get: [getRelated()],
create: [],
update: [],
patch: [],
remove: []
},

after: {
all: [],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
},

error: {
all: [],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
}
};
钩子(Hook)相关
module.exports = function (options = {}) {
return async (context) => {
const { include, ...query } = context.params.query;

if (include) {


const AssociatedModel = context.app.services.policyholders.Model;
context.params.sequelize = {
include: [{ model: AssociatedModel }],
raw: false
};


// Update the query to not include `include`
context.params.query = query;
}

return context;
};
};

最佳答案

答案也是

 "$policyholder.Name%": {
$like: `%somename%`,
},
然后在羽毛服务中将字符串添加到白名单数组

关于mysql - Feathers sequelize 按关联列查找,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67174591/

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