输入 PostgreSQL 末尾出现无用的语法错误

问题描述 投票:0回答:1

是的,像这样的问题有数百个,但是,它们都有不同的语法错误。 Postgres 的语法检查没有给我任何关于这一点的提示。如果我用假常量替换 ID,则插入语句在语法上可以正常工作。我的错误在于我如何在此处添加

do $$ declare begin end $$
构造。

do $$
declare new_inspection_template_id uuid;
begin

insert into inspection_template
  (name, revision, description, product, notes)
values (
  'QUAL349 RevC',
  1,  
  'Quality checks for beveling.',
  'sheet',
  'There are no notes.'
) returning inspection_template_id into new_inspection_template_id;

insert into workflow (station, inspection_template_id)
values ('beveling', new_inspection_template_id);

end $$; 

在我的 Rust-sqlx 迁移中运行它,如下所示:

sqlx::migrate!().run(&mut dbc).await?;

此代码正在运行多次插入,并在

./migrations
目录中找到的多个文件中创建默认值。

我收到此错误

错误:执行迁移时:从数据库返回错误:输入末尾出现语法错误

版本:

psql(PostgreSQL)15.6(Ubuntu 15.6-0ubuntu0.23.10.1)

我也尝试了一下,在输入末尾得到了完全相同的语法错误

with temp as (
  insert into inspection_template
    (name, revision, description, product, notes)
  values (
    'QUAL349 RevC',
    1,  
    'Quality checks for beveling.',
    'sheet',
    'There are no notes.'
  ) returning inspection_template_id
)
insert into workflow (station, inspection_template_id)
select 'beveling', inspection_template_id from temp;
postgresql rust plpgsql rust-sqlx
1个回答
-3
投票

表插入查询始终返回整数数据类型。 因此,将变量

new_inspection_template_id
的数据类型更改为
int
inspection_template.revision (int)
数据类型

所以,这是更新的 Pl/pgSQL 查询:

do $$
  declare 
     new_inspection_template_id inspection_template.revision%TYPE;
     -- or
     -- new_inspection_template_id int;
  begin
      insert into inspection_template
                (name, revision, description, product, notes)
          values (
              'QUAL349 RevC',
              1,  
              'Quality checks for beveling.',
              'sheet',
              'There are no notes.'
        ) returning revision into new_inspection_template_id;

      insert into workflow 
             (station, inspection_template_id)
      values ('beveling', new_inspection_template_id);
end; $$;

源代码:

© www.soinside.com 2019 - 2024. All rights reserved.