You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rust SQLx迁移时PostgreSQL报末尾输入语法错误求助

PostgreSQL语法错误排查问题

尽管存在大量类似问题,但本次语法错误具有特殊性,PostgreSQL未提供有效提示。将ID替换为常量时插入语句语法正常,怀疑问题出在do $$ declare begin end $$结构的使用上。

测试用PL/pgSQL代码

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目录下多文件的创建与插入语句,执行时触发错误:

error: while executing migrations: error returned from database: syntax error at end of input

使用的PostgreSQL版本:

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

尝试改用WITH语句写法,仍出现完全相同的错误:

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;

内容的提问来源于stack exchange,提问作者mmachenry

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 01:51:12