#[derive(Identifiable, Queryable)]
#[table_name = "product_category"]
pub struct ProductCategory {
id: i32,
name: String,
}
#[derive(Queryable, Identifiable, Associations)]
#[belongs_to(ProductCategory, foreign_key="upper_category_id")]
#[belongs_to(ProductCategory, foreign_key="lower_category_id")]
#[table_name = "product_category_rollup"]
pub struct ProductCategoryRollup {
id: i32,
upper_category_id: i32,
lower_category_id: i32,
}
error[E0119]: conflicting implementations of trait `diesel::associations::BelongsTo<entities::ProductCategory>` for type `entities::ProductCategoryRollup`:
--> src/entities.rs:29:35
|
29 | #[derive(Queryable, Identifiable, Associations)]
| ^^^^^^^^^^^^
| |
| first implementation here
| conflicting implementation for `entities::ProductCategoryRollup`
|
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)
拥有多个外国键引用同一张表的正确方法是什么?这是柴油中尚未解决的一些固有限制吗?
特征定义是:
BelongsTo
是相关类型的结果,而不是通用参数,给定的
pub trait BelongsTo<Parent> {
type ForeignKey: Hash + Eq;
type ForeignKeyColumn: Column;
fn foreign_key(&self) -> Option<&Self::ForeignKey>;
fn foreign_key_column() -> Self::ForeignKeyColumn;
}
我一直在研究和研究柴油,如上所述,由于定义了
ForeignKey
特征的方式,因此出现了此问题。
绕过这一点的方法是做类似以下操作:ForeignKeyColumn
我已经注意到,关于可查询的实现,我有相当大的代码重复,但是我不希望通过引入另一个结构,该结构包含一个实现
Child
特征的单个字段。现在来了有趣的部分。我已经注意到为什么会出现,并在柴油Github存储库中打开了一个。如果解决这个问题,我将相应地更新此答案,以显示实现同一事物的更好方法。
从这里开始解决方法:https://github.com/diesel-rs/diesel/issues/89#issuecomment-429547936
BelongsTo<Parent>