Diesel derived Insertable导致不明确的关联类型错误



所以我试着按照本教程构建一个rust驱动的API。我的models.rs看起来像这样:

use diesel;
use diesel::prelude::*;
use diesel::sqlite::SqliteConnection as slc;

use crate::schema::{Recipe as RecipeTable,
RecipeMatchIngredient as RecipeMatchIngredientTable,
AllergyMatchIngredient as AllergyMatchIngredientTable,
DietMatchIngredient as DietMatchIngredientTable,
Ingredient as IngredientTable};
use crate::schema::{
Recipe::dsl::Recipe as AllRecipe,
RecipeMatchIngredient::dsl::RecipeMatchIngredient as AllRecipeMatchIngredient,
AllergyMatchIngredient::dsl::AllergyMatchIngredient as AllAllergyMatchIngredient,
DietMatchIngredient::dsl::DietMatchIngredient as AllDietMatchIngredient,
Ingredient::dsl::Ingredient as AllIngredient
};

#[derive(Queryable)]
pub struct Recipe {
pub id: u64,
pub associated_author_id: u64,
pub meal_type: String,
pub cuisine: String,
pub estimated_ttc: u64,
pub difficulty: u64,
pub count_upvotes: u64,
pub count_reports: u64,
}
impl Recipe {
}
#[derive(Insertable)]
#[table_name="Recipe"]
pub struct NewRecipe {
pub associated_author_id: u64,
pub meal_type: String,
pub cuisine: String,
pub estimated_ttc: u64,
pub difficulty: u64,
pub count_upvotes: u64,
pub count_reports: u64,
}
//==================================================================================================
#[derive(Queryable)]
pub struct Ingredient {
pub id: u64,
pub name: String,
pub cost_eur_cents: u64,
pub calories: u64,
}

#[derive(Insertable)]
#[table_name="Ingredient"]
pub struct NewIngredient {
pub name: String,
pub cost_eur_cents: u64,
pub calories: u64,
}
//==================================================================================================
#[derive(Queryable)]
pub struct RecipeMatchIngredient {
pub link_id: u64, // diesel enforces to have primary key
pub recipe_id: u64,
pub ingredient_id: u64
}

#[derive(Insertable)]
#[table_name="RecipeMatchIngredient"]
pub struct NewRecipeMatchIngredient {
pub recipe_id: u64,
pub ingredient_id: u64
}
//==================================================================================================
#[derive(Queryable)]
pub struct AllergyMatchIngredient {
pub link_id: u64, // diesel enforces to have primary key
pub allergy: String,
pub ingredient_id: u64
}

#[derive(Insertable)]
#[table_name="AllergyMatchIngredient"]
pub struct NewAllergyMatchIngredient {
pub allergy: String,
pub ingredient_id: u64
}
//==================================================================================================
#[derive(Queryable)]
pub struct DietMatchIngredient {
pub link_id: u64, // diesel enforces to have primary key
pub diet: String,
pub ingredient_id: u64,
}

#[derive(Insertable)]
#[table_name="DietMatchIngredient"]
pub struct NewDietMatchIngredient {
pub diet: String,
pub ingredient_id: u64,
}

,我的up.sql是这样的:

-- Your SQL goes here
CREATE TABLE Recipe (
id INTEGER PRIMARY KEY NOT NULL,
associated_user_id INTEGER NOT NULL,
meal_type VARCHAR NOT NULL,
cuisine VARCHAR NOT NULL,
estimated_ttc INTEGER NOT NULL,
difficulty INTEGER NOT NULL,
count_upvotes INTEGER NOT NULL,
count_reports INTEGER NOT NULL
);

CREATE TABLE Ingredient (
id INTEGER PRIMARY KEY NOT NULL,
name VARCHAR NOT NULL,
cost_eur_cents INTEGER NOT NULL,
calories INTEGER NOT NULL
);

CREATE TABLE RecipeMatchIngredient (
link_id INTEGER PRIMARY KEY NOT NULL,
recipe_id INTEGER NOT NULL,
ingredient_id INTEGER NOT NULL,
FOREIGN KEY(ingredient_id) REFERENCES Ingredient(id),
FOREIGN KEY(recipe_id) REFERENCES Recipe(id)
);

CREATE TABLE AllergyMatchIngredient (
link_id INTEGER PRIMARY KEY NOT NULL,
allergy VARCHAR NOT NULL,
ingredient_id INTEGER NOT NULL,
FOREIGN KEY(ingredient_id) REFERENCES Ingredient(id)
);

CREATE TABLE DietMatchIngredient (
link_id INTEGER PRIMARY KEY NOT NULL,
diet VARCHAR NOT NULL,
ingredient_id INTEGER NOT NULL,
FOREIGN KEY(ingredient_id) REFERENCES Ingredient(id)
);

但是会产生以下错误:

error[E0223]: ambiguous associated type
--> srcmodels.rs:38:10
|
38 | #[derive(Insertable)]
|          ^^^^^^^^^^ help: use fully-qualified syntax: `<models::Recipe as Trait>::table`
|
= note: this error originates in the derive macro `Insertable` (in Nightly builds, run with -Z macro-backtrace for more info)

我认为也许错误暗示了一些阴影项目在#[table_name=]宏,但快速测试与一个新的数据库模式没有给出任何结果。

正确的变体是使用来自schema.rs文件的表模块。根据您的导入,这将导致以下调整后的结构定义:

#[derive(Insertable)]
#[table_name="RecipeTable"]
pub struct NewRecipe {
pub associated_author_id: u64,
pub meal_type: String,
pub cuisine: String,
pub estimated_ttc: u64,
pub difficulty: u64,
pub count_upvotes: u64,
pub count_reports: u64,
}

查看"All about insert "关于如何使用柴油插入的更多细节的指南。

最新更新