我有两张桌子,products并且subscriptions:
CREATE TABLE products (
id bigint NOT NULL,
title character varying(75),
description text,
manufacturer_id bigint,
created_at timestamp without time zone,
updated_at timestamp without time zone,
mpn text,
visible boolean DEFAULT false NOT NULL
);
CREATE TABLE subscriptions (
id bigint NOT NULL,
product_id bigint NOT NULL,
user_id bigint NOT NULL,
created_at timestamp without time zone,
updated_at timestamp without time zone
);
Run Code Online (Sandbox Code Playgroud)
在我的应用程序中,我通常需要知道某个产品的订阅者数量,而在应用程序中所有正确位置使用此逻辑是很棘手的。所以我想products用已经包含该信息的视图替换该表。所以我这样做了:
ALTER TABLE ONLY products
RENAME TO products_raw;
CREATE VIEW products AS …Run Code Online (Sandbox Code Playgroud)