【发布时间】:2020-10-02 11:12:17
【问题描述】:
我正在构建一个食谱书应用程序。我只是为了练习在 Postgresql 中工作:
postgres=# select version();
version
------------------------------------------------------------------------------------------------------------------
PostgreSQL 12.2 (Debian 12.2-2.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
(1 row)
我有几个表用于存储基于配方的信息;一个与成分列表有关:
CREATE TABLE ingredient (
id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
name TEXT NOT NULL,
quantity NUMERIC NOT NULL,
unit TEXT,
display_order INT NOT NULL
);
我正在编写一个名为 save_recipe 的函数,目前看起来是这样的
CREATE OR REPLACE PROCEDURE save_recipe
(
first_name COOK.FIRST_NAME%TYPE,
last_name COOK.LAST_NAME%TYPE,
email COOK.EMAIL%TYPE,
recipe_name RECIPE.NAME%TYPE,
recipe_cook_time RECIPE.COOK_TIME%TYPE,
recipe_preface RECIPE.PREFACE%TYPE,
instructions RECIPE.INSTRUCTIONS%TYPE,
ingred INGREDIENT
)
AS $$
DECLARE
cook_id COOK.ID%TYPE;
recipe_id RECIPE.ID%TYPE;
BEGIN
INSERT INTO cook (first_name, last_name, email)
VALUES (first_name, last_name, email)
RETURNING id INTO cook_id;
INSERT INTO recipe (name, cook_id, created_at, cook_time, instructions, preface)
VALUES (recipe_name, cook_id, now(), recipe_cook_time, instructions, recipe_preface)
RETURNING id INTO recipe_id;
INSERT INTO ingredient (name, quantity, unit, display_order)
VALUES (ingred.name, ingred.quantity, ingred.unit, ingred.display_order);
COMMIT;
RAISE NOTICE 'Cook ID : %', cook_id;
RAISE NOTICE 'Recipe ID : %', recipe_id;
END;
$$
LANGUAGE plpgsql;
但我在创建成分文字时遇到了问题(如果这是正确的词)。这是我目前能做的最好的:
CALL save_recipe(
first_name => 'Joe',
last_name => 'Fresh',
email => 'joe@loblaws.com',
recipe_name => 'Cherry Pie',
recipe_cook_time => '1 hour',
recipe_preface =>'I love cherry pie.',
instructions => ARRAY['Make.', 'Bake.', 'Eat.'],
ingred => (0, 'Cherry', 20, 'small, pitted', 1)
);
我希望 ingred 成为一个数组,但我更担心的是我需要填充 ingredient.id,即使我在插入过程中忽略了它(因为我想使用 Postgres 提供的生成的 ID )。有没有我可以使用的结构/类型,我不需要像这样指定一个虚拟 ID(最终可以是 ARRAY 类型)。
提前致谢。
【问题讨论】:
-
我以前读过这个SO answer,但我希望有更好的东西。
标签: postgresql stored-procedures plpgsql postgresql-12