iT邦幫忙

2024 iThome 鐵人賽

DAY 6
0
Modern Web

一些讓你看來很強的 ORM - prisma系列 第 6

Day06. 一些讓你看來很強的 ORM - prisma (schema)上

  • 分享至 

  • xImage
  •  

今天要來吧補充一下作天提到的 schema 內容~

schema

postgres 中,一個類似於 workspace 的概念,就是 schema 他可以區隔不同的 index 不同的 table 等等,其目的是讓你好管理整個 DBdata flow

使先我們先進去 SQL

>psql
psql (14.10 (Homebrew), server 15.2)
WARNING: psql major version 14, server major version 15.
         Some psql features might not work.
Type "help" for help.

接著打 \dn 去查看看當前有哪些 schema 預設情況會有一個 public ,所以在不使用我們自己定的 schema 前提,我們都是用 public 當作當前的 schema

> danny=# \dn;
  List of schemas
  Name  |  Owner
--------+----------
 public | postgres
(1 row)

所以以下的寫法是一樣的意思。

> select * from posts;
> select * from public.posts;

然後我們先 create 一個 schema

>danny=# CREATE SCHEMA hr;
CREATE SCHEMA

在打一次 dn 你會看到成功新增一個 schema 了。

>danny=# \dn;
  List of schemas
  Name  |  Owner
--------+----------
 hr     | danny
 public | postgres
(2 rows)

那要怎麼看到當前的 schema 是什麼呢?可以打以下的指令去看,你會看到我們當前就是 public

>danny=# select CURRENT_SCHEMA;
 current_schema
----------------
 public
(1 row)

如果要切換 schema 可以打 SET SEARCH_PATH=YOUR_SCHEMA_NAME 去切換。

>danny=# SET SEARCH_PATH=hr;
SET

你會看到成功切換 schmea

>danny=# select CURRENT_SCHEMA;
 current_schema
----------------
 hr
(1 row)

這時候你打 \dt 去看當前的 table ,你會發現因為你在新的 schema 中所以你完全是一個新的 workspace 自然也不會有任何的 table 紀錄。

>danny=# \dt;
Did not find any relations.

之後我們先 create 一個 table

CREATE table test100(name varchar(20),id int);

你會看到現在的 schema 就成功紀錄一個新 table 了。

>danny=# \dt;
        List of relations
 Schema |  Name   | Type  | Owner
--------+---------+-------+-------
 hr     | test100 | table | danny
(1 row)

那上面說道 schema 是切換不同工作區,所以 table 這些都是獨立分開的,那要怎麼驗證這件事,很簡單就先切換成 publicschema 看看。

danny=# SET SEARCH_PATH=public;
SET

你會發現根本沒有 test100 這個 table

>danny=# \dt;
                List of relations
 Schema |        Name         | Type  |  Owner
--------+---------------------+-------+----------
 public | Account             | table | danny
 public | CategoriesOnPosts   | table | danny
 public | Category            | table | danny
 public | Conversation        | table | danny
 public | Message             | table | danny
 public | Post                | table | danny
 public | Session             | table | danny
 public | User                | table | danny
 public | VerificationToken   | table | danny
 public | _ConversationToUser | table | danny
 public | _prisma_migrations  | table | postgres
 public | _seeners            | table | danny
 public | timestampTest       | table | danny
(13 rows)

所以自然也不能 select

>danny=# select * from test100;
ERROR:  relation "test100" does not exist
LINE 1: select * from test100;

最後當你要 delete 一個 schema 可以直接打 drop

>danny=# drop schema hr;

但你會發現你完全不能 drop 原因是 schema 已經有 depend on 特定的 table 了,所以如果要 drop 已經使用過的 schema 顯然是不行的。

>danny=# drop schema hr;
ERROR:  cannot drop schema hr because other objects depend on it
DETAIL:  table hr.test100 depends on schema hr
HINT:  Use DROP ... CASCADE to drop the dependent objects too.

如果要刪除已經使用過的 schema 你必須要加上 cascades 這樣才能成功刪除。

>danny=# drop schema hr cascade;
NOTICE:  drop cascades to table hr.test100
DROP SCHEMA

以上就是今天的內容希望對大家有幫助~

大家如果有問題可以來小弟的群組討論~

✅ 前端社群 :
https://lihi3.cc/kBe0Y


上一篇
Day05. 一些讓你看來很強的 ORM - prisma ( Database Adapter)
下一篇
Day07. 一些讓你看來很強的 ORM - prisma (schema)中
系列文
一些讓你看來很強的 ORM - prisma13
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言