Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

建立數據庫模型以支持多種事件類型 #11

Closed
gibbs-shih opened this issue Dec 12, 2023 · 3 comments · Fixed by #23
Closed

建立數據庫模型以支持多種事件類型 #11

gibbs-shih opened this issue Dec 12, 2023 · 3 comments · Fixed by #23
Assignees
Labels
3 enhancement New feature or request

Comments

@gibbs-shih
Copy link
Collaborator

gibbs-shih commented Dec 12, 2023

建立初步數據庫模型

@gibbs-shih gibbs-shih added documentation Improvements or additions to documentation 3 labels Dec 12, 2023
@gibbs-shih gibbs-shih self-assigned this Dec 12, 2023
@gibbs-shih gibbs-shih added enhancement New feature or request and removed documentation Improvements or additions to documentation labels Dec 12, 2023
@gibbs-shih
Copy link
Collaborator Author

gibbs-shih commented Dec 28, 2023

  1. 新分支 setting postgres

  2. 安裝Postgres、pgAdmin4 並完成連接

  1. 安裝dotenv 及設定.env
  • npm install dotenv
  • 將.env 放入.gitignore, 設定.env 放入兩個資料庫連結
    截圖 2023-12-28 下午3 44 11(2)
  • 設定.env.example 留下連結範例
    截圖 2023-12-28 下午3 47 52(2)
  • 在main.ts加入判別邏輯
    截圖 2023-12-28 下午3 54 23(2)
  1. 除原本的sqlite, 設定第二個資料庫
  • schema.prisma 設定
    截圖 2023-12-28 下午4 06 38(2)
  1. 在每個model裡面加入 @@Datasource(“要使用的datasource”), 建立關聯, table name開頭大寫
    截圖 2023-12-28 下午4 39 40(2)

  2. 關聯table
    範例:evidences.chain_id > chains.id (多對一)
    截圖 2023-12-29 上午11 22 14(2)

  3. 刪除migrations資料, 重新建立

  • npx prisma migrate dev --name init

@gibbs-shih
Copy link
Collaborator Author

出現錯誤:error: Attribute not known: "@datasource".
查不能這樣設計 要改成@@Schema('xx'), 關聯要另寫一行, datasource要多設計schemas
ref1. https://www.prisma.io/docs/orm/reference/prisma-schema-reference#schema
ref2. prisma/prisma#1122
範例:
截圖 2023-12-29 下午3 02 56(2)
截圖 2023-12-29 下午3 02 45(2)

@gibbs-shih
Copy link
Collaborator Author

錯誤原因:
一個schema不能設定兩個data source
之前是用sqlite測試 現在全部要轉到雲端的postgresql

  1. 設定好.env連線資訊
  2. 留下postgresql 的 datasource 這裡先把relation都拿掉 以先建起來為目標
    截圖 2023-12-29 下午5 18 08(2)
  3. 刪除migrtations資料夾及dev.db
  4. 使用npx prisma db push --schema=./prisma/schema.prisma 將目前的schema在不用建新資料庫的狀況下推到遠端的資料庫
  5. 指令npm run start:dev 試爬資料
  6. 成功創建資料表及儲存資料
截圖 2023-12-29 下午5 26 32

taking 8 hr

@gibbs-shih gibbs-shih linked a pull request Jan 3, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3 enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants