缺少对 PostgreSQL 中类型值进行约束的 FROM 子句条目

mat*_*boy 4 postgresql

我对以下脚本有疑问:

CREATE DOMAIN shop.amount AS numeric(8,2) DEFAULT 0 NOT NULL CHECK (value > 0::numeric);

CREATE TYPE shop.money AS (
   m_amount shop.amount,
   m_currency shop.currency
 );


CREATE TABLE shop.order_item
 (
   oi_id bigserial NOT NULL,
   oi_order_id bigint NOT NULL,
   oi_article_id bigint NOT NULL,
   oi_article_price shop.money NOT NULL,
   CONSTRAINT oi_pk_id PRIMARY KEY (oi_id),
   CONSTRAINT oi_fk_article_id FOREIGN KEY (oi_article_id)
       REFERENCES shop.article (a_id) MATCH SIMPLE
       ON UPDATE NO ACTION ON DELETE NO ACTION,
   CONSTRAINT oi_pk_order_id FOREIGN KEY (oi_order_id)
       REFERENCES shop."order" (o_id) MATCH SIMPLE
       ON UPDATE NO ACTION ON DELETE NO ACTION,
   CONSTRAINT price_cannot_be_less_than_ten CHECK (oi_article_price.m_amount >= 10::numeric)
 );
Run Code Online (Sandbox Code Playgroud)

当我使用 PGAdmin 执行脚本时,我得到

ERROR:  missing FROM-clause entry for table "oi_article_price"
Run Code Online (Sandbox Code Playgroud)

我猜这个错误与约束有关。怎么了?

小智 5

您需要将域名放在括号中:

CONSTRAINT price_cannot_be_less_than_ten 
  CHECK ( (oi_article_price).m_amount >= 10)
Run Code Online (Sandbox Code Playgroud)

  • @mat_boy:避免语法歧义。从错误消息中可以看出,Postgres 期望标识符是不带括号的 *table*。在这里阅读手册:https://www.postgresql.org/docs/current/static/sql-expressions.html#FIELD-SELECTION (2认同)