gpt4 book ai didi

sequelize.js - 无法在 Feathers-Sequelize 中使用一对一关联填充 PostgreSQL 表

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

我有两张 table :用户账户 .一个用户有一个帐户,反之亦然。这是我的两个模型:

用户型号

const Sequelize = require('sequelize');
const DataTypes = Sequelize.DataTypes;

module.exports = function (app) {
const sequelizeClient = app.get('sequelizeClient');
const user = sequelizeClient.define('users', {

// omitting the definition for brevity
});


user.associate = function (models) {

user.hasOne(models.accounts);

// I could get things to work when I made it a one to many
// but that's not what I want.
// user.hasMany(models.accounts, { foreignKey: 'user_id' });
};

return user;
};

账号型号
const Sequelize = require('sequelize');
const DataTypes = Sequelize.DataTypes;

module.exports = function (app) {
const sequelizeClient = app.get('sequelizeClient');
const account = sequelizeClient.define('accounts', {

// omitting the definition for brevity

});

account.associate = function (models) {

account.belongsTo(models.users, {foreignKey: 'user_id'});

// I can't get groups and roles working either but that's
// another question...
account.belongsToMany(models.groups, {
through: 'accountsingroups',
foreignKey: 'accountid'
});
account.belongsToMany(models.roles, {
through: 'accountsinroles',
foreignKey: 'accountid'
});
};

return account;
};

从这些模型中,我得到一个 用户表和 账户 具有名为 user_id 的附加列的表.

我有一系列创建前的钩子(Hook): hashPassword , hashSecurityQuestions , 和 relateAccount .前两个工作完美无缺。第三个是我遇到问题的地方。这是我的 relateAccount钩:

创建 Hook 前的关联帐户
module.exports = function (options = {}) {
return function (context) {
const AccountModel = context.app.services.accounts.Model;
context.params.sequelize = {
include: [{ model: AccountModel }]
};
};
};

通过上述设置,我可以将新用户插入 用户表使用 Postman 但 账户 表仍然是空的。这是一个示例 postman 请求:
{
"username": "doctordonna",
"firstname": "Donna",
"lastname": "Noble",
"email": "besttempinchiswick@tardis.com",
"password": "ood1!",
"accounts":
{
"questiononeid": "1a3ccccb-42ff-4553-9e54-824022414f50",
"questiononeanswer": "100 words per minute",
"questiontwoid": "6f9b0a38-0b1f-46dd-8726-2c9ff3566686",
"questiontwoanswer": "Adipose Industries",
"questionthreeid": "95c7f66f-63ef-48b2-b2b4-d73d383077e8",
"questionthreeanswer": "Vesuvius",
"questionfourid": "ff94887b-6015-4b84-8259-4801b2b404a3",
"questionfouranswer": "Time Vortex",
"isapproved": true,
"lastactivitydate": null,
"lastlogindate": null,
"lastpasswordchangeddate": null,
"isonline": false,
"islockedout": false,
"lastlockedoutdate": null,
"failedpasswordattemptcount": 0,
"failedpasswordattemptwindowstart": null,
"isdeactivated": false,
"deactivated_at": null
}

}

就像我上面所说的,当我将关联更改为 时,它可以完美运行。一对多 但是,我希望这是 一对一 .如果我删除 user.hasOne(models.accounts);来自 用户型号我收到一条错误消息 error: SequelizeEagerLoadingError: accounts is not associated to users!
显然我做错了什么,我想我不理解我在 Sequelize 文档和 Feathers 文档中读到的内容。

最佳答案

啊。这对我来说是一个非常愚蠢的错误。

在我的请求中,我需要制作 accounts单数- account .所以我的请求应该是这样的:

{
"username": "doctordonna",
"firstname": "Donna",
"lastname": "Noble",
"email": "besttempinchiswick@tardis.com",
"password": "ood1!",
"account":
{
"questiononeid": "1a3ccccb-42ff-4553-9e54-824022414f50",
"questiononeanswer": "100 words per minute",
"questiontwoid": "6f9b0a38-0b1f-46dd-8726-2c9ff3566686",
"questiontwoanswer": "Adipose Industries",
"questionthreeid": "95c7f66f-63ef-48b2-b2b4-d73d383077e8",
"questionthreeanswer": "Vesuvius",
"questionfourid": "ff94887b-6015-4b84-8259-4801b2b404a3",
"questionfouranswer": "Time Vortex",
"isapproved": true,
"lastactivitydate": null,
"lastlogindate": null,
"lastpasswordchangeddate": null,
"isonline": false,
"islockedout": false,
"lastlockedoutdate": null,
"failedpasswordattemptcount": 0,
"failedpasswordattemptwindowstart": null,
"isdeactivated": false,
"deactivated_at": null
}

}

关于sequelize.js - 无法在 Feathers-Sequelize 中使用一对一关联填充 PostgreSQL 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54946210/

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