如何在Postgres中设置hasone和属于关系
我有两个表是用户表,另一个是邀请表。
module.exports = (sequelize, DataTypes) => {
class User extends Model {
}
User.init({
uuid:{
type:DataTypes.UUID,
defaultValue:DataTypes.UUIDV4,
primaryKey:true
},
name: {
type:DataTypes.STRING,
allowNull:false
},
email: {
type:DataTypes.STRING,
allowNull:false
},
},
}, {
sequelize,
tableName: 'users',
modelName: 'User',
timestamps: false,
classMethods: {
associate:function(models){
User.hasOne(models.Invitation )
}
}
});
return User;
};
与
'use strict';
const {
Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class Invitation extends Model {
}
Invitation.init({
uuid:{
type:DataTypes.UUID,
defaultValue:DataTypes.UUIDV4
},
user_name: {
type:DataTypes.STRING,
allowNull:false
},
userTo: {
type:DataTypes.STRING,
//allowNull:false
},
}, {
sequelize,
tableName: 'invitations',
modelName: 'Invitation',
timestamps: false,
classMethods: {
associate:function(models){
Invitation.belongsTo(models.User,{foreignKey:'userTo'})
}
}
});
return Invitation;
};
邀请表中的USERTO具有 用户表的值相同的值。 我想用Findall命令获取所有数据,但它显示了ERRRO 我尝试运行续集查询,但显示 sequelizeEagerloadingError 错误 我知道SQL中的查询,但面临问题。
SELECT User.name, Invitation.CustomerName
FROM User
INNER JOIN Invitation ON User.uuid = Invitation.userTo;
I have two tables one is User Table and another one is the Invitation table.
module.exports = (sequelize, DataTypes) => {
class User extends Model {
}
User.init({
uuid:{
type:DataTypes.UUID,
defaultValue:DataTypes.UUIDV4,
primaryKey:true
},
name: {
type:DataTypes.STRING,
allowNull:false
},
email: {
type:DataTypes.STRING,
allowNull:false
},
},
}, {
sequelize,
tableName: 'users',
modelName: 'User',
timestamps: false,
classMethods: {
associate:function(models){
User.hasOne(models.Invitation )
}
}
});
return User;
};
and
'use strict';
const {
Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class Invitation extends Model {
}
Invitation.init({
uuid:{
type:DataTypes.UUID,
defaultValue:DataTypes.UUIDV4
},
user_name: {
type:DataTypes.STRING,
allowNull:false
},
userTo: {
type:DataTypes.STRING,
//allowNull:false
},
}, {
sequelize,
tableName: 'invitations',
modelName: 'Invitation',
timestamps: false,
classMethods: {
associate:function(models){
Invitation.belongsTo(models.User,{foreignKey:'userTo'})
}
}
});
return Invitation;
};
userTo in the Invitation table has the same value as the User.UUID table.
I want to fetch all data with findAll command but it is showing errro
I tried to run sequelize query but it showed SequelizeEagerLoadingError error
I know the query in SQL but facing a problem with this.
SELECT User.name, Invitation.CustomerName
FROM User
INNER JOIN Invitation ON User.uuid = Invitation.userTo;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果这是邀请模型中的外键,请不要在邀请模型中添加为字段,因为您已经添加了该表的关联。因此,您可以从邀请模型中删除它。
更新后,查看是否有任何错误
If this is your foreign key in Invitation model, don't add that as field in Invitation model as you already added association for the table. So you can remove that from Invitation model.
Once update, see if there is any error
最后解决了错误。我将关联设置在模型的顶部
,然后将USERTO的数据类型更改为datatypes.uuid .UUID .BEACAUSE DATATYPES的共同属性的数据类型应该相同。
Finally solved the error. I set the association at the top of the models
and change the datatype of userTo to DataTypes.UUID .beacause datatype of common attribute should be same .