我尝试向订单表中已经存在的列添加约束,即它是foring键:
ALTER TABLE IF EXISTS public.orders DROP COLUMN IF EXISTS location_id;
ALTER TABLE IF EXISTS public.orders
ADD COLUMN location_id bigint
CONSTRAINT orders_location_id_fkey FOREIGN KEY (location_id)
REFERENCES public.locations (id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION
1条答案
按热度按时间aiqt4smr1#
不能在列定义中使用“表约束条件”语法。请使用“列约束条件”语法:
请参见the documentation中的语法图:
***column_constraint***
[...]
和
***column_constraint***
是:[ CONSTRAINT***constraint_name***]
[...]
REFERENCES***reftable***[ (***refcolumn***) ] [ MATCH FULL | MATCH PARTIAL | MATCH SIMPLE ]
[ ON DELETE***referential_action***] [ ON UPDATE***referential_action***] }