数据库-Postgres
我有以下关系:
users <—>> users_organizations <<—> organizations
模式:
table! {
organizations (id) {
id -> Int4,
name -> Varchar,
}
}
table! {
users (id) {
id -> Int4,
name -> Varchar,
email -> Varchar,
password -> Varchar,
}
}
table! {
users_organizations (id, user_id, organization_id) {
id -> Int4,
user_id -> Int4,
organization_id -> Int4,
}
}
型号:
#[derive(Identifiable, Queryable, Debug, Serialize, Deserialize)]
pub struct Organization {
pub id: i32,
pub name: String,
}
#[derive(Identifiable, Queryable, PartialEq, Debug, Serialize, Deserialize)]
pub struct User {
pub id: i32,
pub name: String,
pub email: String,
pub password: String,
}
#[derive(Identifiable, Queryable, Debug, Associations, Serialize, Deserialize)]
#[belongs_to(User)]
#[belongs_to(Organization)]
#[table_name = "users_organizations"]
pub struct UserOrganization {
pub id: i32,
pub user_id: i32,
pub organization_id: i32,
}
我想创建一个组织。为了支持这种关系,我必须手动将用户和组织的ID添加到users_organizations表中。有没有更好的方法来实现这种关系?
let new_organization = NewOrganization { name: &msg.name };
let organization = insert_into(organizations::table)
.values(&new_organization)
.get_result::(conn)
.map_err(|_| error::ErrorInternalServerError(“Error creating organization”))?;
let new_user_org = NewUserOrganizationIDs {
user_id: msg.user_id,
organization_id: organization.id,
};
insert_into(users_organizations::table)
.values(&new_user_org)
.get_result::<UserOrganization>(conn)
.map_err(|_| error::ErrorInternalServerError("Error creating user-organization data"))
这里有同样的问题。在选择所有与用户(反之亦然)相关的组织的情况下,我想到了以下代码:
let user = users::table.filter(users::id.eq(&msg.user_id))
.get_result::<User>(conn)
.map_err(|_| error::ErrorNotFound("User doesn't exist"))?;
let user_organizations = UserOrganization::belonging_to(&user)
.get_results::<UserOrganization>(conn)
.map_err(|_| error::ErrorNotFound("User doesn't have any organization"))?;
let mut organization_ids = vec![];
for user_org in &user_organizations {
organization_ids.push(user_org.organization_id);
}
organizations::table.filter(organizations::id.eq_any(organization_ids))
.get_results::<Organization>(conn)
.map_err(|_| error::ErrorNotFound("Organizations don't exist"))
答案 0 :(得分:3)
此答案来自@SRugina和@weiznich(针对该问题进行编辑和修改)的the Diesel chat。
我通常将belonging_to
和join
结合起来,所以像这样:
UserOrganization::belonging_to(&organizations)
.inner_join(user::table)
belonging_to_many
的东西?否,belonging_to_many
不存在,因为Diesel不会尝试向您隐藏数据库。这样做会在您要处理复杂或非标准的事情时引起问题。根据您的实际用例,也可以选择将所有三个表都连接起来。
inner_join
?您有三个表:users
,organizations
和user_organizations
,并且您想要获取特定用户的所有组织。
有两种变体。第一个变体仅是一个查询,但如果要对所有用户执行此操作,则可能与所需的数据布局不匹配:
users::table
.inner_join(user_organizations::table.inner_join(organizations::table))
.filter(users::id.eq(user_id))
.select(organizations::all_columns)
.load::<Organization>(conn)?;
第二个变体允许使用内置的关联API将每个用户的结果分组:
let user = users::table
.find(user_id)
.first::<User>(conn)?;
UserOrganization::belonging_to(&user)
.inner_join(organizations::table)
.select(organizations::all_columns)
.load::<Organization>(conn)?;
插入需要三个单独的插入。我们不会试图隐藏它,因为最后由用户选择如何在插入失败的情况下处理数据一致性。使用交易是一种常见的选择。