gorm与foreignKey引用有许多关系

huangapple go评论79阅读模式
英文:

gorm has many relation with foreignKey reference

问题

我的目标是在gorm中实现一个“has many”关系。

我不想有任何生成的ID,所以我故意没有在我的结构体中使用gorm.Model。

我设置了两个结构体:

type Application struct {
    Name         string        `json:"name" gorm:"primaryKey"`
    Description  string        `json:"description"`
    Translations []Translation `json:"titles" gorm:"foreignKey:ApplicationName;references:Name"`
}

type Translation struct {
    ApplicationName string `json:"applicationName" gorm:"primaryKey"`
    Locale          string `json:"locale" gorm:"primaryKey"`
    Value           string `json:"value"`
}

Translation.ApplicationName应该是Applications的外键。

(Translation.ApplicationName + Translation.Locale)是Translations的主键。

创建一个应用程序后,我得到了以下错误:

ON CONFLICT子句与任何主键或唯一约束不匹配
[0.065ms] [rows:0] INSERT INTO translations
(application_name,locale,value) VALUES
("postedApplication1","de-DE","deutsch"),("postedApplication1","de-AT","AT")
ON CONFLICT (application_name,locale) DO UPDATE SET
application_name=excluded.application_name

ON CONFLICT子句与任何主键或唯一约束不匹配
[0.531ms] [rows:0] UPDATE applications SET
description="postedDescription3" WHERE name = "postedApplication1"
[GIN] 2021/08/27 - 11:23:00 | 200 | 841.953μs | 127.0.0.1 |
POST "/applications"

有人知道我做错了什么吗?

英文:

My goal is to archive a "has many" relation with gorm

I don't want to have any generated IDs so I intentionally did not use gorm.Model in my structs

I set up my two structs like:

type Application struct {
  Name         string        `json:"name" gorm:"primaryKey"`
  Description  string        `json:"description"`
  Translations []Translation `json:"titles" gorm:"foreignKey:ApplicationName;references:Name"`
}

type Translation struct {
  ApplicationName string `json:"applicationName" gorm:"primaryKey"`
  Locale          string `json:"locale" gorm:"primaryKey"`
  Value           string `json:"value"`
}

Translation.ApplicationName should be the foreignKey to Applications

(Translation.ApplicationName + Translation.Locale) the primary key for Translations

After creating an application

{
  "name" : "postedApplication1",
  "description" : "postedDescription3",
  "titles" : [
    {
        "locale": "de-DE",
        "value":"deutsch"
    },
     {
        "locale": "de-AT",
        "value":"AT"
    }
  ]
}

I got following error

> ON CONFLICT clause does not match any PRIMARY KEY or UNIQUE constraint
> [0.065ms] [rows:0] INSERT INTO translations
> (application_name,locale,value) VALUES
> ("postedApplication1","de-DE","deutsch"),("postedApplication1","de-AT","AT")
> ON CONFLICT (application_name,locale) DO UPDATE SET
> application_name=excluded.application_name

and

> ON CONFLICT clause does not match any PRIMARY KEY or UNIQUE constraint
> [0.531ms] [rows:0] UPDATE applications SET
> description="postedDescription3" WHERE name = "postedApplication1"
> [GIN] 2021/08/27 - 11:23:00 | 200 | 841.953µs | 127.0.0.1 |
> POST "/applications"

Someone any idea what I'm doing wrong

答案1

得分: 1

已解决 qx.X,p

一切都正确!

在我安装了 vscode-sqlite 并检查了数据库之后,我才意识到 sqlite 表格不像我设计的那样。

问题是 AutoMigrate 在开发过程中进行了很多更改,导致产生了一个无效的状态。

database.AutoMigrate(&models.Application{}, &models.Translation{})

我不得不删除 sqlite 的 "gorm.db" 文件并重新启动应用程序。

英文:

SOLVED qx.X,p

Everything was correct!

After I installed vscode-sqlite and inspected the database I just recognized that the sqlite tables were not like I designed them

The problem was that AutoMigrate which produced an invalid state due changing a lot during developing

database.AutoMigrate(&models.Application{}, &models.Translation{})

I had to delete sqlite "gorm.db" file and restart the application

huangapple
  • 本文由 发表于 2021年8月27日 18:20:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/68951784.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定