Sql Sequelize belongToMany关联未通过表工作
我在PostgreSQL上使用Sequelize来存储属于组织的用户。组织拥有用户可以访问的设备。所以本质上,用户也通过他们的组织拥有设备 我设置了每个设备都与使用组织id的组织相关联,以及每个用户都与使用组织id的组织相关联。我正在尝试使用Sequelize进行设置以正确读取它。我尽量不去写自定义查询,但如果最后不得不写的话也没关系 我正在尝试获取与用户ID关联的所有设备。当我尝试运行findAll(…)命令时,Sequelize会打印出这个疯狂的查询,并出现一个错误。它输出此查询,然后输出一个空集:Sql Sequelize belongToMany关联未通过表工作,sql,node.js,postgresql,orm,sequelize.js,Sql,Node.js,Postgresql,Orm,Sequelize.js,我在PostgreSQL上使用Sequelize来存储属于组织的用户。组织拥有用户可以访问的设备。所以本质上,用户也通过他们的组织拥有设备 我设置了每个设备都与使用组织id的组织相关联,以及每个用户都与使用组织id的组织相关联。我正在尝试使用Sequelize进行设置以正确读取它。我尽量不去写自定义查询,但如果最后不得不写的话也没关系 我正在尝试获取与用户ID关联的所有设备。当我尝试运行findAll(…)命令时,Sequelize会打印出这个疯狂的查询,并出现一个错误。它输出此查询,然后输出一
SELECT
"receiver"."receiver_id" AS "id",
"receiver"."organization_id" AS "organizationID",
"receiver"."identifier",
"receiver"."secret",
"receiver"."iterations",
"receiver"."sodium",
"receiver"."algorithm",
"receiver"."created",
"receiver"."modified",
"receiver"."deleted",
"receiver"."organization_id",
"users"."user_id" AS "users.id",
"users"."password" AS "users.password",
"users"."sodium" AS "users.sodium",
"users"."email" AS "users.email",
"users"."organization_id" AS "users.organizationID",
"users"."iterations" AS "users.iterations",
"users"."algorithm" AS "users.algorithm",
"users"."created" AS "users.created",
"users"."modified" AS "users.modified",
"users"."deleted" AS "users.deleted",
"users"."organization_id" AS "users.organization_id",
"users.organizations"."created" AS "users.organizations.created",
"users.organizations"."modified" AS "users.organizations.modified",
"users.organizations"."organization_id" AS "users.organizations.organization_id"
FROM "receivers" AS "receiver"
INNER JOIN (
"organizations" AS "users.organizations"
INNER JOIN "users" AS "users"
ON "users"."user_id" = "users.organizations"."organization_id")
ON "receiver"."receiver_id" = "users.organizations"."organization_id"
AND ("users"."deleted" IS NULL AND "users"."user_id" = 2)
WHERE "receiver"."deleted" IS NULL;
如何更好地编写定义或代码
非常感谢
Sequelize中的我的表定义:
var organization = sequelize.define( 'organization', {
'id': {
type: Sequelize.BIGINT,
field: 'organization_id',
allowNull: false,
primaryKey: true,
autoIncrement: true
},
'name' : {
type: Sequelize.STRING( 256 ),
field: 'name',
allowNull: false,
validate: {
notEmpty: true
}
}
}, {
'createdAt' : 'created',
'updatedAt' : 'modified',
'deletedAt' : 'deleted',
'tableName' : 'organizations',
'paranoid' : true
} );
var user = sequelize.define( 'user', {
'id': {
type: Sequelize.BIGINT,
field: 'user_id',
allowNull: false,
primaryKey: true,
autoIncrement: true
},
'password': {
type: Sequelize.STRING( 64 ),
field: 'password',
allowNull: false,
validate: {
notEmpty: true
}
},
'sodium': {
type: Sequelize.STRING( 64 ),
field: 'sodium',
allowNull: false,
validate: {
notEmpty: true
}
},
'email' : {
type: Sequelize.STRING( 64 ),
field: 'email',
allowNull: false,
validate: {
notEmpty: true
}
},
'organizationID' : {
type: Sequelize.BIGINT,
field: 'organization_id',
allowNull: false,
validate: {
notEmpty: true
}
},
'iterations' : {
type: Sequelize.INTEGER,
field: 'iterations',
allowNull: false,
validate: {
notEmpty: true
}
},
'algorithm' : {
type: Sequelize.STRING( 8 ),
field: 'algorithm',
allowNull: false,
defaultValue: 'sha256'
}
}, {
'createdAt' : 'created',
'updatedAt' : 'modified',
'deletedAt' : 'deleted',
'tableName' : 'users',
'paranoid' : true
} );
var receiver = sequelize.define( 'receiver', {
'id': {
type: Sequelize.BIGINT,
field: 'receiver_id',
allowNull: false,
primaryKey: true,
autoIncrement: true
},
'organizationID': {
type: Sequelize.BIGINT,
field: 'organization_id',
allowNull: false,
validate: {
notEmpty: true
}
},
'identifier': {
type: Sequelize.STRING( 64 ),
field: 'identifier',
allowNull: false,
validate: {
notEmpty: true
}
},
'secret' : {
type: Sequelize.STRING( 64 ),
field: 'secret',
allowNull: false,
validate: {
notEmpty: true
}
},
'iterations' : {
type: Sequelize.INTEGER,
field: 'iterations',
allowNull: false,
validate: {
notEmpty: true
}
},
'sodium': {
type: Sequelize.STRING( 64 ),
field: 'sodium',
allowNull: false,
validate: {
notEmpty: true
}
},
'algorithm' : {
type: Sequelize.STRING( 8 ),
field: 'algorithm',
allowNull: false,
defaultValue: 'sha256'
}
}, {
'createdAt' : 'created',
'updatedAt' : 'modified',
'deletedAt' : 'deleted',
'tableName' : 'receivers',
'paranoid' : true
} );
// Organizations have users and users have organizations
organization.hasMany( user, { 'foreignKey' : 'organization_id' } );
user.belongsTo( organization, { 'foreignKey' : 'organization_id' } );
// Organizations have receivers
organization.hasMany( receiver, { 'foreignKey' : 'organization_id' } );
receiver.belongsTo( organization, { 'foreignKey' : 'organization_id' } );
// Receivers to users
user.belongsToMany( receiver, { 'through' : 'organizations', 'foreignKey' : 'organization_id' } );
receiver.belongsToMany( user, { 'through' : 'organizations', 'foreignKey' : 'organization_id' } );
我用于查询的代码:
// Get the devices for this person
db.receiver.findAll( {
'include' : [
{
'model' : db.user,
'where' : { 'id' : 2 }
}
]
} )
.complete( function( error, result ) {
if( error ) {
console.log( error );
}
else {
console.log( result );
}
} );
尝试以下操作,它选择与where语句匹配的用户,并包括与其关联的组织,而该组织又包括与其关联的设备,因此您应该最终得到与该用户关联的设备
// Organizations have users
user.belongsTo(organization);
// Organizations have receivers
receiver.belongsTo(organization);
// Get the devices for this person
db.user.find( {
'include' : [
{model: db.organization,
include: [model: db.receiver]
}
]
'where' : { 'id' : 2 }
} )
.complete( function( error, result ) {
if( error ) {
console.log( error );
}
else {
console.log( result );
}
} );
如果您使用的是下划线id字段名称,如organization\u id,则可以在创建模型时指定“下划线:true”,这样在创建关联时就不必指定外键字段。为什么在userdevice表中同时包含userID和OrganizationID?感谢您撰写此评论。这让我意识到我有额外的代码和一些问题。这是一个单独表格的一部分,与此问题无关。我希望我写的更清楚!好建议。非常感谢。我按照你说的去做,而是选择了用户,包括了组织,然后是接收者,我在接收者上做了查找,包括了组织,然后是用户,然后我在用户上做了一个where,包括了ID。无论哪种方式,它使用的查询看起来都很粗糙,但如果它能工作,我想它会工作的!再次感谢。@mikealeonetti很高兴它对你有用!只是想知道你为什么决定做逆向选择?我的意思是接收者,然后是组织,然后是使用where语句的用户?我的目标主要是获取用户可以关联的所有接收者设备。获取时,我并不真正需要有关组织或用户的信息。因此,对我来说,在receivers表上执行find/select以首先获取这些数据更为合理。否则,我必须在查找中获得结果,获得组织,然后获得接收者。如果这有道理的话:D。