Gorm自动迁移可能不按照我认为的方式工作?

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

Gorm AutoMigrate Possibly Not Working As I Think it Should?

问题

我已经使用Gorm一年了,它一直表现得非常好。然而,我正在尝试重构我的数据库模式,但出于某种原因,Gorm的迁移结果并不如预期。我的应用程序是一个多租户应用程序,所以我有一个客户端ID的概念,它使用的是一个POSTGRES数据库("PostgreSQL 13.3 on x86_64-pc-linux-gnu, compiled by Debian clang version 10.0.1 , 64-bit")。问题是,我愚蠢地给一些结构体/列命名时加了公司名称的前缀,让我们称之为COM。所以,例如,我的客户端结构体原来是COMClient,现在只是client(为简单起见,我删除了一些数据)。

type Client struct {
	ID      string
	Name    string
	Address string
}

然后,我有一个以下的Base结构体:

type BaseClient struct {
	ID       string
	ClientID string `gorm:"type:uuid"`
	Client   *Client
}

再次强调,基础结构体有COMClientID和COMClient字段。这个基础客户端结构体将在我所有其他需要与客户端表建立外键关系的结构体中使用。例如,我的用户结构体如下:

type ClientUser struct {
	**BaseClient**
	FirstName string
	LastName  string
	Email     string `gorm:"uniqueIndex"`
	Password  string
}

简而言之,我正在将COMClient重构为Client。然后,在应用程序启动时,我会迁移所有的结构体,如下所示:

db.AutoMigrate(&Client{})
db.AutoMigrate(&ClientUser{})
// 其他结构体的迁移...

问题是,并不是每个表都创建了一个名为client_id的列,而这些表都有一个指向旧客户端表'com_clients'的外键。

然而,如果我在一个新的数据库上运行整个迁移过程,一切都会按预期创建...

我已经思考了几个小时,为什么有些表创建了该列,而有些表没有。

谢谢。

英文:

I have been using Gorm for a year and it has been working pretty good. However, I am trying to refactor my db schema and for some reason Gorm is not migrating as expected. My application is a multi-tenant application so I have a concept of a client id, which is using a POSTGRES Db ("PostgreSQL 13.3 on x86_64-pc-linux-gnu, compiled by Debian clang version 10.0.1 , 64-bit"). The issue is that I foolishly name some of my structs/columns with a prefix of our company name, lets call the company COM. So, for example, my client struct WAS COMClient, and now it is just client (I have removed some data for simplicity)

type Client struct {
ID String
Name string
Address string
}

I then had a Base struct of the following
type BaseClient struct {
ID string
ClientID string gorm:"type:uuid"
Client *Client
}

Again, the base struct has fields of COMClientID and COMClient. This base client struct would be used in all of my other structs, where I needed to have a foreign key back to my client table. For example,
my user struct would be:

type ClientUser struct {
BaseClient
FirstName string
LastName string
Email string gorm:"uniqueIndex"
Password string
}

In a nutshell, I am refactoring COMClient => Client. I then migrate all my structs when the application starts like the following:

db.AutoMigrate(&Client{})
db.AutoMigrate(&ClientUser{})
etc....

The issue is that a column client_id is not created in every table which has a foreign key to the clients table. Those tables all have foreign keys to the old client table 'com_clients'.

However, if I run my whole migration on a new db, everything gets created as it should be...

I have been wracking my head for hours, on why SOME tables have the column created and some do not.

Thank You

答案1

得分: 1

好的,以下是翻译好的内容:

好吧,这有点尴尬,但我找到了问题所在...我只是发帖,以防其他人犯同样的错误。

如果你的数据库迁移不如预期,可以使用Debug()运行AutoMigrate,即db.Debug().AutoMigrate(tables...)。

当我决定使用Gorm时,我选择使用他们的约定,然而,如果你犯了一个拼写错误(就像我一样),事情就不会正常工作。

应该是 - 非常简单
ClientID string
Client *Client

我犯的错误
ClientID string
Clent *Client

我忘记了client中的'i'。

英文:

Ok, this is a little embarrassing, but I figured out my issue...I am only posting this, in case someone else makes the same mistake.

If your db is not migrating as expected, run the AutoMigrate with Debug(), so db.Debug().AutoMigrate(tables...).

When I decided to use Gorm, I made the decision to use their conventions, however, if you make a typo (like I did), things won't work.

Should Be - Very Simple
ClientID string
Client *Client

What I Did
ClientID string
Clent *Client

I forgot the 'i' in client.

huangapple
  • 本文由 发表于 2021年8月14日 05:06:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/68778344.html
匿名

发表评论

匿名网友

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

确定