我有一張用戶表。我想編寫一個使用 id 獲取數據的 API。我不斷收到以下錯誤。請注意,數據庫已經滿了。我想創建一個 get api,在其中傳遞 id 并獲取該行作為結果。我嘗試了一些方法,但我不斷收到此錯誤。數據庫架構-deadpool=# \d user_data Table "public.user_data" Column | Type | Collation | Nullable | Default--------------------+--------------------------+-----------+----------+--------------------------------------- id | integer | | not null | nextval('user_data_id_seq'::regclass) created_at | timestamp with time zone | | | updated_at | timestamp with time zone | | | deleted_at | timestamp with time zone | | | name | text | | | age | text | | | gender | text | | | party_code | text | | | criminal_cases | text | | | number_of_cases | text | | | serious_ipc_counts | text | | | ipc_details | text | | | education_level | text | | | movable_assets | text | | | immovable_assets | text | | | total_assets | text | | | total_liabilities | text | | | pan_given | text | | | election | text | | | constituency | text | | |Indexes: "user_data_pkey" PRIMARY KEY, btree (id) "idx_user_data_deleted_at" btree (deleted_at)
1 回答

人到中年有點甜
TA貢獻1895條經驗 獲得超7個贊
您沒有設置包級dbvar,因為您使用:=.
如果出現錯誤,請退出mainfunc,這樣您就不會繼續使用 nil db。
var db *gorm.DB
func main() {
var err error
db, err = gorm.Open()
if err != nil {
log.Fatalf("unable to connect to database: %s", err.Error())
}
}
- 1 回答
- 0 關注
- 140 瀏覽
添加回答
舉報
0/150
提交
取消