Node.js 如何使用子表中的数据在Sequelize中查找数据
因此,我使用Sequelize创建了这个查询,以便在数据库中查找配置文件,但我希望能够使用存储在不同(子)表/模型中的条件查找结果,而不仅仅是在主表中 这里是我的代码的功能摘录: 续集模型设置:Node.js 如何使用子表中的数据在Sequelize中查找数据,node.js,sequelize.js,Node.js,Sequelize.js,因此,我使用Sequelize创建了这个查询,以便在数据库中查找配置文件,但我希望能够使用存储在不同(子)表/模型中的条件查找结果,而不仅仅是在主表中 这里是我的代码的功能摘录: 续集模型设置: const Profile = sequelize.define('profiles', { // Database columns: profile_id: { type: Sequelize.STRING, primaryKey
const Profile = sequelize.define('profiles',
{ // Database columns:
profile_id: {
type: Sequelize.STRING,
primaryKey: true
},
user_name: {
type: Sequelize.STRING,
unique: true
}
});
const Email = sequelize.define('profiles_emails',
{ // Database columns:
entry_id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
owner_id: {
type: Sequelize.STRING,
},
email_address: {
type: Sequelize.STRING,
}
});
const Phone = sequelize.define('profiles_phones',
{ // Database columns:
entry_id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
owner_id: {
type: Sequelize.STRING,
},
phone_number: {
type: Sequelize.STRING,
}
});
Profile.hasMany(Email,
{ // Options:
foreignKey: 'owner_id'
});
Profile.hasMany(Phone,
{ // Options:
foreignKey: 'owner_id'
});
Profile.hasOne(Password,
{ // Options:
foreignKey: 'owner_id'
});
// TODO: Finding by email and phone does not work because finding in other tables than the parent table is not programed.
Profile
.findAll({
where: {
[Op.or]: [
{ profile_id: '665C70C2A9F9' },
{ user_name: 'user1' },
{ email_address: 'user1@mail.com' },
{ phone_number: '554111222' }]
},
include: [Phone, Email], // Tables that I want to fetch data from.
})
.then(function(data) {
if (data) { console.log('Number of rows found: ' + data.length, JSON.stringify(data)) }
else { console.log('No entries found.') }
})
.catch(function(data) { console.error('An error occurred while making the query:\n' + data) });
建立关联:
const Profile = sequelize.define('profiles',
{ // Database columns:
profile_id: {
type: Sequelize.STRING,
primaryKey: true
},
user_name: {
type: Sequelize.STRING,
unique: true
}
});
const Email = sequelize.define('profiles_emails',
{ // Database columns:
entry_id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
owner_id: {
type: Sequelize.STRING,
},
email_address: {
type: Sequelize.STRING,
}
});
const Phone = sequelize.define('profiles_phones',
{ // Database columns:
entry_id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
owner_id: {
type: Sequelize.STRING,
},
phone_number: {
type: Sequelize.STRING,
}
});
Profile.hasMany(Email,
{ // Options:
foreignKey: 'owner_id'
});
Profile.hasMany(Phone,
{ // Options:
foreignKey: 'owner_id'
});
Profile.hasOne(Password,
{ // Options:
foreignKey: 'owner_id'
});
// TODO: Finding by email and phone does not work because finding in other tables than the parent table is not programed.
Profile
.findAll({
where: {
[Op.or]: [
{ profile_id: '665C70C2A9F9' },
{ user_name: 'user1' },
{ email_address: 'user1@mail.com' },
{ phone_number: '554111222' }]
},
include: [Phone, Email], // Tables that I want to fetch data from.
})
.then(function(data) {
if (data) { console.log('Number of rows found: ' + data.length, JSON.stringify(data)) }
else { console.log('No entries found.') }
})
.catch(function(data) { console.error('An error occurred while making the query:\n' + data) });
执行:
const Profile = sequelize.define('profiles',
{ // Database columns:
profile_id: {
type: Sequelize.STRING,
primaryKey: true
},
user_name: {
type: Sequelize.STRING,
unique: true
}
});
const Email = sequelize.define('profiles_emails',
{ // Database columns:
entry_id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
owner_id: {
type: Sequelize.STRING,
},
email_address: {
type: Sequelize.STRING,
}
});
const Phone = sequelize.define('profiles_phones',
{ // Database columns:
entry_id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
owner_id: {
type: Sequelize.STRING,
},
phone_number: {
type: Sequelize.STRING,
}
});
Profile.hasMany(Email,
{ // Options:
foreignKey: 'owner_id'
});
Profile.hasMany(Phone,
{ // Options:
foreignKey: 'owner_id'
});
Profile.hasOne(Password,
{ // Options:
foreignKey: 'owner_id'
});
// TODO: Finding by email and phone does not work because finding in other tables than the parent table is not programed.
Profile
.findAll({
where: {
[Op.or]: [
{ profile_id: '665C70C2A9F9' },
{ user_name: 'user1' },
{ email_address: 'user1@mail.com' },
{ phone_number: '554111222' }]
},
include: [Phone, Email], // Tables that I want to fetch data from.
})
.then(function(data) {
if (data) { console.log('Number of rows found: ' + data.length, JSON.stringify(data)) }
else { console.log('No entries found.') }
})
.catch(function(data) { console.error('An error occurred while making the query:\n' + data) });
“email_address”和“phone_number”列存储在不同的子表中,并且Sequelize关联已经成功建立
从实用角度解释:我希望能够通过电话和电子邮件获取用户,即使这些标准在不同的相关模块中
你知道怎么做吗?我已经试过了,但没有成功:
根据Vivek Doshi的建议进行更新:
const Profile = sequelize.define('profiles',
{ // Database columns:
profile_id: {
type: Sequelize.STRING,
primaryKey: true
},
user_name: {
type: Sequelize.STRING,
unique: true
}
});
const Email = sequelize.define('profiles_emails',
{ // Database columns:
entry_id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
owner_id: {
type: Sequelize.STRING,
},
email_address: {
type: Sequelize.STRING,
}
});
const Phone = sequelize.define('profiles_phones',
{ // Database columns:
entry_id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
owner_id: {
type: Sequelize.STRING,
},
phone_number: {
type: Sequelize.STRING,
}
});
Profile.hasMany(Email,
{ // Options:
foreignKey: 'owner_id'
});
Profile.hasMany(Phone,
{ // Options:
foreignKey: 'owner_id'
});
Profile.hasOne(Password,
{ // Options:
foreignKey: 'owner_id'
});
// TODO: Finding by email and phone does not work because finding in other tables than the parent table is not programed.
Profile
.findAll({
where: {
[Op.or]: [
{ profile_id: '665C70C2A9F9' },
{ user_name: 'user1' },
{ email_address: 'user1@mail.com' },
{ phone_number: '554111222' }]
},
include: [Phone, Email], // Tables that I want to fetch data from.
})
.then(function(data) {
if (data) { console.log('Number of rows found: ' + data.length, JSON.stringify(data)) }
else { console.log('No entries found.') }
})
.catch(function(data) { console.error('An error occurred while making the query:\n' + data) });
下面是应用的第一个建议:
Profile
.findAll({
where: {
[Op.or]: [
{ profile_id: '665C70C2A9F9' },
{ user_name: 'user1' },
{ '$profiles_emails.email_address$': 'user1@mail.com' },
{ '$profiles_phones.phone_number$': '554111222' }
],
},
include: [Phone, Email], // Tables that I want to fetch data from.
limit: 5 // Limit of profiles to look for.
})
.then(function(data) {
if (data) { console.log('Number of rows found: ' + data.length, JSON.stringify(data)) }
else { console.log('No entries found.') }
})
.catch(function(data) { console.error('An error occurred while making the query:\n' + data) });
Profile
.findAll({
where: {
[Op.or]: [
{ profile_id: '665C70C2A9F9' },
{ user_name: 'user1' },
],
},
include: [{
model : Phone,
where : { phone_number: '554111222' } ,
required : false
}, {
model : Email,
where : { email_address: 'user1@mail.com' } ,
required : false
}],
limit: 5 // Limit of profiles to look for.
})
.then(function(data) {
if (data) { console.log('Number of rows found: ' + data.length, JSON.stringify(data)) }
else { console.log('No entries found.') }
})
.catch(function(data) { console.error('An error occurred while making the query:\n' + data) });
这会导致错误:
Executing (default): SELECT `profiles`.*, `profiles_phones`.`entry_id` AS `profiles_phones.entry_id`, `profiles_phones`.`owner_id` AS `profiles_phones.owner_id`, `profiles_phones`.`phone_number` AS `profiles_phones.phone_number`, `profiles_phones`.`verified` AS `profiles_phones.verified`, `profiles_phones`.`modified_time` AS `profiles_phones.modified_time`, `profiles_phones`.`modified_by` AS `profiles_phones.modified_by`, `profiles_emails`.`entry_id` AS `profiles_emails.entry_id`, `profiles_emails`.`owner_id` AS `profiles_emails.owner_id`, `profiles_emails`.`email_address` AS `profiles_emails.email_address`, `profiles_emails`.`verified` AS `profiles_emails.verified`, `profiles_emails`.`modified_time` AS `profiles_emails.modified_time`, `profiles_emails`.`modified_by` AS `profiles_emails.modified_by` FROM (SELECT `profiles`.`profile_id`, `profiles`.`user_name`, `profiles`.`modified_time`, `profiles`.`modified_by` FROM `profiles` AS `profiles` WHERE (`profiles`.`profile_id` = '665C70C2A9F9' OR `profiles`.`user_name` = 'user1' OR `profiles_emails`.`email_address` = 'user1@mail.com' OR `profiles_phones`.`phone_number` = '554111222') LIMIT 5) AS `profiles` LEFT OUTER JOIN `profiles_phones` AS `profiles_phones` ON `profiles`.`profile_id` = `profiles_phones`.`owner_id` LEFT OUTER JOIN `profiles_emails` AS `profiles_emails` ON `profiles`.`profile_id` = `profiles_emails`.`owner_id`;
An error occurred while making the query:
SequelizeDatabaseError: Unknown column 'profiles_emails.email_address' in 'where clause'
下面是应用的第二个建议:
Profile
.findAll({
where: {
[Op.or]: [
{ profile_id: '665C70C2A9F9' },
{ user_name: 'user1' },
{ '$profiles_emails.email_address$': 'user1@mail.com' },
{ '$profiles_phones.phone_number$': '554111222' }
],
},
include: [Phone, Email], // Tables that I want to fetch data from.
limit: 5 // Limit of profiles to look for.
})
.then(function(data) {
if (data) { console.log('Number of rows found: ' + data.length, JSON.stringify(data)) }
else { console.log('No entries found.') }
})
.catch(function(data) { console.error('An error occurred while making the query:\n' + data) });
Profile
.findAll({
where: {
[Op.or]: [
{ profile_id: '665C70C2A9F9' },
{ user_name: 'user1' },
],
},
include: [{
model : Phone,
where : { phone_number: '554111222' } ,
required : false
}, {
model : Email,
where : { email_address: 'user1@mail.com' } ,
required : false
}],
limit: 5 // Limit of profiles to look for.
})
.then(function(data) {
if (data) { console.log('Number of rows found: ' + data.length, JSON.stringify(data)) }
else { console.log('No entries found.') }
})
.catch(function(data) { console.error('An error occurred while making the query:\n' + data) });
其结果是:
Executing (default): SELECT `profiles`.*, `profiles_phones`.`entry_id` AS `profiles_phones.entry_id`, `profiles_phones`.`owner_id` AS `profiles_phones.owner_id`, `profiles_phones`.`phone_number` AS `profiles_phones.phone_number`, `profiles_phones`.`verified` AS `profiles_phones.verified`, `profiles_phones`.`modified_time` AS `profiles_phones.modified_time`, `profiles_phones`.`modified_by` AS `profiles_phones.modified_by`, `profiles_emails`.`entry_id` AS `profiles_emails.entry_id`, `profiles_emails`.`owner_id` AS `profiles_emails.owner_id`, `profiles_emails`.`email_address` AS `profiles_emails.email_address`, `profiles_emails`.`verified` AS `profiles_emails.verified`, `profiles_emails`.`modified_time` AS `profiles_emails.modified_time`, `profiles_emails`.`modified_by` AS `profiles_emails.modified_by` FROM (SELECT `profiles`.`profile_id`, `profiles`.`user_name`, `profiles`.`modified_time`, `profiles`.`modified_by` FROM `profiles` AS `profiles` WHERE (`profiles`.`profile_id` = 'user1' OR `profiles`.`user_name` = 'jorge') AND ( SELECT `owner_id` FROM `profiles_phones` AS `profiles_phones` WHERE (`profiles_phones`.`phone_number` = '9' AND `profiles_phones`.`owner_id` = `profiles`.`profile_id`) LIMIT 1 ) IS NOT NULL LIMIT 5) AS `profiles` INNER JOIN `profiles_phones` AS `profiles_phones` ON `profiles`.`profile_id` = `profiles_phones`.`owner_id` AND `profiles_phones`.`phone_number` = '9' LEFT OUTER JOIN `profiles_emails` AS `profiles_emails` ON `profiles`.`profile_id` = `profiles_emails`.`owner_id` AND `profiles_emails`.`email_address` = 'user1@mail.com';
这个可以工作并查找行。但它会获取与“main”模型的结果相关联的结果(即,通过userid/user_name查找数据,然后根据匹配的其他数据(电子邮件和电话)来确定这些结果是否伴随)。这不是我现在想要的,我希望能够使用其他表中的条件/列找到一个包含所有数据和相同结构的完整配置文件。这就是使用Sequelize获得预期结果的方法
Profile
.findAll({
where: {
[Op.or]: [
{ profile_id: '665C70C2A9F9' },
{ user_name: 'user1' }
]
},
include: [ {
model : Phone ,
where : { phone_number: '554111222' } ,
required : false // as you want it in OR relation relation
}, {
model : Email
where : { email_address: 'user1@mail.com' } ,
required : false // as you want it in OR relation relation
}]
})
或
Profile
.findAll({
where: {
[Op.or]: [
{ profile_id: '665C70C2A9F9' },
{ user_name: 'user1' },
{ '$profiles_emails.email_address$': 'user1@mail.com' },
{ '$profiles_phones.phone_number$': '554111222' }]
},
include: [Phone, Email], // Tables that I want to fetch data from.
})
非常感谢。但不幸的是,它似乎也不起作用。第一个示例不会导致错误,但仍然无法从列email\u address和phone\u number中找到结果。在最后一个例子中,我得到了“SequelizeDatabaseError:Unknown column'profiles\u emails.email\u address'In'where子句'”。请发布您的错误,Sequalizei生成的原始查询用您的例子的结果更新了我的问题,如果有什么不清楚的地方,请告诉我:)提供的第二种解决方案的问题是,如果添加偏移量并限制where对象中使用的子表的列将是未知列。