【发布时间】:2023-01-03 23:01:16
【问题描述】:
我有两个表用户和文件。它们以这样一种方式相关,即每个文档必须属于使用一对多关系的用户。当我尝试更新文档时出现以下错误
错误:插入或更新表“文档”违反了外键 约束“fk_users_documents”(SQLSTATE 23503)
这是我的结构定义和更新功能
type User struct { gorm.Model Name string Email string Password string Documents []Document } type Document struct { gorm.Model Name string UserID uint } //Update document by id func (h handler)UpdateDocument(w http.ResponseWriter, r *http.Request) { // once again, we will need to parse the path parameters var updatedDoc Document reqBody, _ := ioutil.ReadAll(r.Body) json.Unmarshal(reqBody, &updatedDoc) var document Document vars := mux.Vars(r) id := vars["id"] if result := Db.First(&updatedDoc, id); result.Error != nil { fmt.Println(result.Error) } document.Name=updatedDoc.Name Db.Save(&document) json.NewEncoder(w).Encode(&updatedDoc) }
【问题讨论】:
标签: postgresql go grails-orm