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:
parent
1f53b8fcc2
commit
d5fa37a9fa
1 changed files with 4 additions and 2 deletions
|
|
@ -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) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue