admin管理员组

文章数量:1125080

I'm developing a multi-tenant app using Rust (diesel as its DB driver) and PostgreSQL. I've decided to use Row Level Security in PostgreSQL to enforce data isolation in the DB layer rather than having to filter on tenant_id in each query. This works well, as in my function to fetch a connection from the AsyncDbPool I just set the right tenant context and don't need to worry about filtering in my queries.

However, as this is enforced in the DB, I'd very much like to abstract my Rust models from having to include tenant_id in them. Is that somehow possible?

I've tried to use AsChangeset to omit the tenant_id from the struct definition:

#[derive(Queryable, Insertable, Serialize, Deserialize, AsChangeset)]
#[diesel(table_name = users)]
pub struct User {
    pub id: Uuid,
    pub email: String,
    pub display_name: Option<String>,
}

but Rust still complains:

this is a mismatch between what your query returns and what your type expects the query to return

the fields in your struct need to match the fields returned by your query in count, order and type

I guess that's because AsChangeset allows me to omit nullable fields from new item creation, but don't really work for filtering fields on selected items.

本文标签: Multitenant Rust dieselPostgreSQL without tenantid in each modelStack Overflow