The trait bound diesel::BelongingToDsl<>
is not implemented for my model
#4022
-
Versions
Feature Flags
Hi! I'm currently experiencing an issue where I'm trying to access This is the snippet of code where this is occuring // src/interface.rs
pub fn get_albums_tracks(album: Albums) {
let connection = &mut establish_db_connection();
let _tracks = AlbumTracks::belonging_to(album).load(connection);
} This is the exact output by rustc error[E0277]: the trait bound `models::AlbumTracks: diesel::BelongingToDsl<models::Albums>` is not satisfied
--> src/interface.rs:36:19
|
36 | let _tracks = AlbumTracks::belonging_to(album).load(connection);
| ^^^^^^^^^^^ the trait `diesel::BelongingToDsl<models::Albums>` is not implemented for `models::AlbumTracks` And this is how I have my models setup // src/models.rs
use crate::schema::*;
use diesel::prelude::*;
...
#[derive(Identifiable, Selectable, Queryable, Insertable, PartialEq, Debug)]
#[diesel(table_name = albums)]
pub struct Albums {
pub albums_name: String,
pub albums_year: i32,
pub cover_path: String,
pub id: i32,
}
#[derive(Identifiable, Selectable, Queryable, Insertable, PartialEq, Debug)]
#[diesel(table_name = tracks)]
pub struct Tracks {
pub id: i32,
pub tracks_name: String,
}
#[derive(Identifiable, Selectable, Insertable, Queryable, Associations, PartialEq, Debug)]
#[diesel(table_name = album_tracks)]
#[diesel(primary_key(albums_id, tracks_id))]
#[diesel(belongs_to(Albums, foreign_key=albums_id))]
#[diesel(belongs_to(Tracks, foreign_key=tracks_id))]
pub struct AlbumTracks {
pub albums_id: i32,
pub tracks_id: i32,
} This is the schema generated by Diesel CLI // src/schema.rs
// @generated automatically by Diesel CLI.
diesel::table! {
album_tracks (albums_id, tracks_id) {
albums_id -> Integer,
tracks_id -> Integer,
}
}
diesel::table! {
albums (id) {
albums_name -> Text,
albums_year -> Integer,
cover_path -> Text,
id -> Integer,
}
}
...
diesel::table! {
tracks (id) {
id -> Integer,
tracks_name -> Text,
}
}
diesel::joinable!(album_tracks -> albums (albums_id));
diesel::joinable!(album_tracks -> tracks (tracks_id));
diesel::allow_tables_to_appear_in_same_query!(
album_tracks,
albums,
artists,
tracks,
); I've still experienced this issue by copying the documentation 1:1, exact same commands from https://diesel.rs/guides/relations.html |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Thanks for writing this question and providing all these details. That makes it much easier to answer the question.
The code for the guides is tested in our CI. It's guaranteed to work, you very likely did not perform a 1:1 copy but missed a important piece.
This is the part of the code that contains the issue. The documentation lists that this trait is only implemented for references to the corresponding parent types, but you pass an owned value here. You can fix that easily by changing the code to pub fn get_albums_tracks(album: Albums) {
let connection = &mut establish_db_connection();
let _tracks = AlbumTracks::belonging_to(&album).load(connection);
} (Notice the single That all written: The error message is really not good for this case. You might want to fill an issue in the rustc issue tracker for that as it should really point out that it would work if you pass an reference in this case. |
Beta Was this translation helpful? Give feedback.
Thanks for writing this question and providing all these details. That makes it much easier to answer the question.
The code for the guides is tested in our CI. It's guaranteed to work, you very likely did not perform a 1:1 copy but missed a important piece.
This is the part of the code that contains the issue. The docu…