Open Ran4 opened 4 years ago
It needs to be
#[derive(Serialize, Deserialize, Insertable, Identifiable, Queryable, PartialEq, Debug)]
#[table_name = "api_keys"]
#[primary_key(uuid)]
pub struct ApiKey {
pub uuid: Uuid,
}
That said: I'm not sure why the corresponding custom derive even accepted the malformed primary key attribute.
Thanks, that solved the issue. Is there any examples of how you would do a transient belonging_to?
E.g. I have an ApiKey, I want all Rights belonging to the ApiKey. I can't do Right::belonging_to(&api_key)
because the belongs_to is on ApiKeyRight, not Right, and I'm not sure how I would specify a belongs_to on Right that combines both ApiKey and ApiKeyRight?
For m:n
relations like this one I normally just use a join for the second relation, so something like ApiKeyRight::belonging_to(&api_key).inner_join(rights::table)
For
m:n
relations like this one I normally just use a join for the second relation, so something likeApiKeyRight::belonging_to(&api_key).inner_join(rights::table)
Wouldn't you have to turn that into a Vec<(ApiKeyRight, Right)>
instead of just a Vec<Right>
? Or is there a way to "collect" into just a Vec<Right>
even if you've joined with another table? I guess I could just pick out a Vec of fields, but then I would have to manually construct the output which would be kind of annoying.
Is there any reason to even use belonging_to
instead of just inner joining all three tables on the two id's?
Just trying to figure out the best practise for this type of somewhat common operation :)
Wouldn't you have to turn that into a Vec<(ApiKeyRight, Right)> instead of just a Vec
? Or is there a way to "collect" into just a Vec even if you've joined with another table? I guess I could just pick out a Vec of fields, but then I would have to manually construct the output which would be kind of annoying.
Correct, the query that I've posted above would yield Vec<(ApiKeyRight, Right)>
. To only get Vec<Right>
simply add a select clause like .select(rights::all_columns)
.
Is there any reason to even use belonging_to instead of just inner joining all three tables on the two id's?
That really depends on what you want to do with the data afterwards. If you try to load for example all rights for all api keys, using the BelongingTo
API is probably a good idea. If you just want the right for a specific key I would probably just go for the raw join variant (and not even brother with including api_key::table
in that query.)
Setup
Versions
Feature Flags
["postgres", "uuidv07"]
Problem Description
I have a setup that looks like this:
Schema.rs:
I'm trying to get all ApiKeyRight:s that belong to ApiKey:
Error message:
What is going on here? ApiKeyRight should be using
Uuid
to compare withApiKey
, right? Where does it expect()
?The latter errors comes from trying to Eq between schema::api_key_rights::columns::api_key_uuid and (), where it should be between schema::api_key_rights::columns::api_key_uuid and schema::api_keys::columns::uuid
I tried doing the ...as BelongingToDsl trick:
but it gives me the same type of errors (
expected (), found &uuid::Uuid
.