Fix recipe save: lastInsertId was undefined, ingredients got null recipe_id

tauri-plugin-sql execute() returns { lastInsertId } but it's unreliable
(returns 0 or undefined for INSERT). New recipe got rid=undefined,
then addRecipeIngredient failed with NOT NULL constraint on recipe_id.

Fix: query actual id after insert with SELECT id ORDER BY id DESC LIMIT 1.
Edit worked because rid came from the existing recipeId parameter.
This commit is contained in:
4aiman 2026-05-29 09:25:30 +03:00
commit d5fa37a9fa

View file

@ -205,12 +205,14 @@ async function getRecipeIngredients(recipeId) {
}
async function saveRecipe(name, cookingMethodId, servings, notes) {
const r = await execute(
await execute(
`INSERT INTO recipes (name, cooking_method_id, servings, notes, created_at)
VALUES (?1, ?2, ?3, ?4, datetime('now'))`,
[name, cookingMethodId ?? null, servings ?? 1, notes ?? null]
);
return r.lastInsertId;
// lastInsertId from tauri-plugin-sql is unreliable; query the actual id
const rows = await select("SELECT id FROM recipes ORDER BY id DESC LIMIT 1");
return rows[0]?.id;
}
async function updateRecipe(id, name, cookingMethodId, servings, notes) {