无法填充 PostgreSQL Table 在 Feathers-Sequelize 中使用一对一关联
Unable to Populate PostgreSQL Table Using a One to One Association in Feathers-Sequelize
我有两个 table:Users 和 Accounts。一个用户有一个帐户,反之亦然。这是我的两个模型:
用户模型
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;
};
从这些模型中我得到一个 Users table 和一个 Accounts table 有一个额外的列称为 user_id
.
我有一系列 before create 钩子:hashPassword
、hashSecurityQuestions
和 relateAccount
。前两个工作完美。第三个是我遇到问题的地方。这是我的 relateAccount
钩子:
relateAccount before create hook
module.exports = function (options = {}) {
return function (context) {
const AccountModel = context.app.services.accounts.Model;
context.params.sequelize = {
include: [{ model: AccountModel }]
};
};
};
通过上述设置,我可以使用 Postman 将新用户插入 Users table,但 Accounts table 仍然是空的。这是一个邮递员请求示例:
{
"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 Model 中删除 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
}
}
我有两个 table:Users 和 Accounts。一个用户有一个帐户,反之亦然。这是我的两个模型:
用户模型
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;
};
从这些模型中我得到一个 Users table 和一个 Accounts table 有一个额外的列称为 user_id
.
我有一系列 before create 钩子:hashPassword
、hashSecurityQuestions
和 relateAccount
。前两个工作完美。第三个是我遇到问题的地方。这是我的 relateAccount
钩子:
relateAccount before create hook
module.exports = function (options = {}) {
return function (context) {
const AccountModel = context.app.services.accounts.Model;
context.params.sequelize = {
include: [{ model: AccountModel }]
};
};
};
通过上述设置,我可以使用 Postman 将新用户插入 Users table,但 Accounts table 仍然是空的。这是一个邮递员请求示例:
{
"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 Model 中删除 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
}
}