Postgresql: 在 ""ViewShifts"" 处或附近出现语法错误 为啥我在下面的查询中会出现此错误?

Posted

技术标签:

【中文标题】Postgresql: 在 ""ViewShifts"" 处或附近出现语法错误 为啥我在下面的查询中会出现此错误?【英文标题】:Postgresql: syntax error at or near ""ViewShifts"" why do i get this error with the query below?Postgresql: 在 ""ViewShifts"" 处或附近出现语法错误 为什么我在下面的查询中会出现此错误? 【发布时间】:2021-05-14 21:22:56 【问题描述】:
    await queryInterface.sequelize.query(`
    CREATE OR REPLACE "ViewShifts" AS
    select
      s.id,
      s.facility_id,
      s.assigned_nurse_id,
      case when u.id is not null
        then jsonb_build_object(
          'id', u.id,
          'name', u.name,
          'rating', u.rating,
          'image', u.image->>'src'
        )
        else null
      end as "assignedNurse",
      case when p.id is not null
        then jsonb_build_object(
          'id', p.id,
          'paymentDate', p."paymentDate",
          'isPaymentDateDefault', p."isPaymentDateDefault",
          'status', p.status,
          'type', p.type,
          'netPay', p."netPay",
          'comment', p.comment,
          'reason', p.reason
        )
        else null
      end as payment,
      p.adjustments as "paymentAdjustments",
      s.role,
      s.type,
      s.unit,
      s."unitDescription",
      s.start_time,
      s.end_time,
      s.rate,
      s.net_pay,
      s.qualifications,
      s.description,
      s.status,
      s."prevStatus",
      s."statusUpdatedAt",
      s."breakTime",
      s.review,
      s."isMinPaymentEnabled",
      s."applicantLocationStatus",
      "checkIn".id as "checkInId",
      "checkIn"."selectedTime" as "checkInTime",
      "checkIn"."createdAt" as "checkInCreatedAt",
      "checkOut".id as "checkOutId",
      "checkOut"."selectedTime" as "checkOutTime",
      "checkOut"."createdAt" as "checkOutCreatedAt",
      "checkOut"."rating" as "ratingFromNurse",
      s."applicantCount" as applicants,
      f.id as "facilityId",
      f.name as "facilityName",
      f.short_name as "facilityShortName",
      f.timezone as "facilityTimezone",
      f."segmentId",
      f."segmentName",
      s."createdAt",
      s."updatedAt",
      s."deletedAt",
      s."cancelledAt"
      s."autoSelectApplicant",
      s."autoSelectApplicantSelected",
      s."autoSelectType",
      from shifts s
      left join facilities f on (s.facility_id = f.id and f."deletedAt" is null)
      left join users u on (s.assigned_nurse_id = u.id)
      left join "CheckEvents" as "checkIn" on (s."actualCheckInId" = "checkIn".id)
      left join "CheckEvents" as "checkOut" on (s."actualCheckOutId" = "checkOut".id)
      left join "Payments" as p on (p."shiftId" = s.id and p.type <> 'other');
    `);

请问“ViewShifts”错误处或附近出现语法错误的原因是什么?任何帮助将不胜感激。

我正在尝试修改现有的 Postgres 视图。我还能怎么办?

请问“ViewShifts”错误处或附近出现语法错误的原因是什么?任何帮助将不胜感激。

我正在尝试修改现有的 Postgres 视图。我还能怎么办?

【问题讨论】:

【参考方案1】:

您的代码中有一些语法错误

CREATE OR REPLACE VIEW "ViewShifts" AS
select
  s.id,
  s.facility_id,
  s.assigned_nurse_id,
  case when u.id is not null
    then jsonb_build_object(
      'id', u.id,
      'name', u.name,
      'rating', u.rating,
      'image', u.image->>'src'
    )
    else null
  end as "assignedNurse",
  case when p.id is not null
    then jsonb_build_object(
      'id', p.id,
      'paymentDate', p."paymentDate",
      'isPaymentDateDefault', p."isPaymentDateDefault",
      'status', p.status,
      'type', p.type,
      'netPay', p."netPay",
      'comment', p.comment,
      'reason', p.reason
    )
    else null
  end as payment,
  p.adjustments as "paymentAdjustments",
  s.role,
  s.type,
  s.unit,
  s."unitDescription",
  s.start_time,
  s.end_time,
  s.rate,
  s.net_pay,
  s.qualifications,
  s.description,
  s.status,
  s."prevStatus",
  s."statusUpdatedAt",
  s."breakTime",
  s.review,
  s."isMinPaymentEnabled",
  s."applicantLocationStatus",
  "checkIn".id as "checkInId",
  "checkIn"."selectedTime" as "checkInTime",
  "checkIn"."createdAt" as "checkInCreatedAt",
  "checkOut".id as "checkOutId",
  "checkOut"."selectedTime" as "checkOutTime",
  "checkOut"."createdAt" as "checkOutCreatedAt",
  "checkOut"."rating" as "ratingFromNurse",
  s."applicantCount" as applicants,
  f.id as "facilityId",
  f.name as "facilityName",
  f.short_name as "facilityShortName",
  f.timezone as "facilityTimezone",
  f."segmentId",
  f."segmentName",
  s."createdAt",
  s."updatedAt",
  s."deletedAt",
  s."cancelledAt",
  s."autoSelectApplicant",
  s."autoSelectApplicantSelected",
  s."autoSelectType"
  from shifts s
  left join facilities f on (s.facility_id = f.id and f."deletedAt" is null)
  left join users u on (s.assigned_nurse_id = u.id)
  left join "CheckEvents" as "checkIn" on (s."actualCheckInId" = "checkIn".id)
  left join "CheckEvents" as "checkOut" on (s."actualCheckOutId" = "checkOut".id)
  left join "Payments" as p on (p."shiftId" = s.id and p.type <> 'other');

【讨论】:

以上是关于Postgresql: 在 ""ViewShifts"" 处或附近出现语法错误 为啥我在下面的查询中会出现此错误?的主要内容,如果未能解决你的问题,请参考以下文章

PostgreSQL:在 Rails 迁移中使用 add_column "after" 选项

" " postgresql 函数处或附近的语法错误

带有 dapper 和 postgresql 的“WHERE x IN y”子句抛出 42601:在 \"$1\" 处或附近出现语法错误

PostgreSQL 中的 数据类型 使用问题

在 PostgreSQL 的对象数组中添加更多数据

我尝试在 laravel 上使用 postgresql 但找不到驱动程序(SQL: select * from "users" where "id" = 4 l