From d5fa37a9faba1e06d6f7fad4ef3a811de4f003ca Mon Sep 17 00:00:00 2001 From: 4aiman Date: Fri, 29 May 2026 09:25:30 +0300 Subject: [PATCH] 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. --- frontend/js/db.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/frontend/js/db.js b/frontend/js/db.js index 4f46c8a..a78e309 100644 --- a/frontend/js/db.js +++ b/frontend/js/db.js @@ -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) {