使 psql 表成为一对多关系时出现语法错误
Syntax error when making psql Tables one-to-many relation
我正在尝试为我的后端创建一些基本数据库 API。
我想要一个与 order 和 order_products 的一对多关系,所以只能有一个 order(id) 但在“order_products”中可以有更多行与相同的“order_id”但我收到语法错误:
“order”处或附近的语法错误
第 2 行:“order_id” int REFERENCES order(id),
^
我不知道为什么(这是在 order_products table)。
CREATE TABLE IF NOT EXISTS "customers" (
"id" SERIAL PRIMARY KEY,
"first_name" varchar,
"last_name" varchar,
"username" varchar,
"password" varchar,
"email" varchar UNIQUE,
"is_admin" boolean
);
CREATE TABLE IF NOT EXISTS "address" (
"customer_id" int REFERENCES customers(id),
"zipcode" int,
"country" varchar,
"city" varchar,
"street_name" varchar,
"stree_number" varchar,
"mobile_number" int
);
CREATE TABLE IF NOT EXISTS "products" (
"id" SERIAL PRIMARY KEY,
"item_name" varchar UNIQUE,
"description" text,
"image_url" varchar,
"price" float
);
CREATE TABLE IF NOT EXISTS "order" (
"customer_id" int REFERENCES customers(id),
"id" SERIAL PRIMARY KEY,
"date_of_purchase" date,
"total_price" int
);
CREATE TABLE IF NOT EXISTS "order_products" (
"order_id" int REFERENCES order(id),
"product_id" int REFERENCES products(id),
"quantity" int,
"total_price" int,
UNIQUE(order_id, product_id)
);
CREATE TABLE IF NOT EXISTS "cart" (
"customer_id" int REFERENCES customers(id),
"id" SERIAL PRIMARY KEY,
"date_of_purchase" date,
"total_price" int
);
CREATE TABLE IF NOT EXISTS "cart_products" (
"cart_id" int REFERENCES cart(id),
"product_id" int REFERENCES products(id),
"quantity" int,
"total_price" int,
UNIQUE(cart_id, product_id)
);
order
是保留关键字,所以需要用双引号总是括起来:
"order_id" int REFERENCES "order"(id),
一般来说,我建议永远不要使用双引号。为此,您需要为 order
table 找到一个不需要引用
的不同名称
我正在尝试为我的后端创建一些基本数据库 API。 我想要一个与 order 和 order_products 的一对多关系,所以只能有一个 order(id) 但在“order_products”中可以有更多行与相同的“order_id”但我收到语法错误:
“order”处或附近的语法错误 第 2 行:“order_id” int REFERENCES order(id), ^ 我不知道为什么(这是在 order_products table)。
CREATE TABLE IF NOT EXISTS "customers" (
"id" SERIAL PRIMARY KEY,
"first_name" varchar,
"last_name" varchar,
"username" varchar,
"password" varchar,
"email" varchar UNIQUE,
"is_admin" boolean
);
CREATE TABLE IF NOT EXISTS "address" (
"customer_id" int REFERENCES customers(id),
"zipcode" int,
"country" varchar,
"city" varchar,
"street_name" varchar,
"stree_number" varchar,
"mobile_number" int
);
CREATE TABLE IF NOT EXISTS "products" (
"id" SERIAL PRIMARY KEY,
"item_name" varchar UNIQUE,
"description" text,
"image_url" varchar,
"price" float
);
CREATE TABLE IF NOT EXISTS "order" (
"customer_id" int REFERENCES customers(id),
"id" SERIAL PRIMARY KEY,
"date_of_purchase" date,
"total_price" int
);
CREATE TABLE IF NOT EXISTS "order_products" (
"order_id" int REFERENCES order(id),
"product_id" int REFERENCES products(id),
"quantity" int,
"total_price" int,
UNIQUE(order_id, product_id)
);
CREATE TABLE IF NOT EXISTS "cart" (
"customer_id" int REFERENCES customers(id),
"id" SERIAL PRIMARY KEY,
"date_of_purchase" date,
"total_price" int
);
CREATE TABLE IF NOT EXISTS "cart_products" (
"cart_id" int REFERENCES cart(id),
"product_id" int REFERENCES products(id),
"quantity" int,
"total_price" int,
UNIQUE(cart_id, product_id)
);
order
是保留关键字,所以需要用双引号总是括起来:
"order_id" int REFERENCES "order"(id),
一般来说,我建议永远不要使用双引号。为此,您需要为 order
table 找到一个不需要引用