插入后无法检索任何关系表。

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

Cannot retrieve any of the relation tables after insertion

问题

我正在使用Golanggorm库,并且我有以下所示的表。我在检索关联表方面遇到了问题,例如当我插入一个新用户时,日志中显示的所有内容都被插入,但是当我尝试检索该用户时,所有应该填充外部表的字段要么是nil,要么是[]。从数据库中我可以看到这些表的值被插入,并且外键实际上也被创建了,那么问题可能是什么?

模型:

type User struct {
	ID          string `json:"id" gorm:"primaryKey"`
	CreatedAt   time.Time
	UpdatedAt   time.Time
	DeletedAt   gorm.DeletedAt `gorm:"index"`
	License     *License       `json:"license"`
	Statistics  []*Statistic   `json:"statistics"`
	App         *App           `json:"app"`
	Disabled    bool           `json:"disabled"`
	EncryptedID string         `json:"encrypted_id"`
}
//在创建后存在于数据库中但无法找到的表之一
type License struct {
	gorm.Model
	Key               string  `json:"key"`
	DesktopHardwareID string  `json:"desktop_hardware_id"`
	MobileHardwareID  *string `json:"mobile_hardware_id"`
	Stripe            *Stripe  `json:"stripe" gorm:"foreignKey:LicenseID"`
	UserID            string  `json:"user_id"`
}

日志:

2021/05/22 22:01:24 C:/Users/sn3ak/go/pkg/mod/github.com/!project!athenaa/database-module@v0.0.0-20210522190032-e0fbf2b16c12/models/user.go:27 SLOW SQL >= 200ms
[282.555ms] [rows:1] INSERT INTO "licenses" ("created_at","updated_at","deleted_at","key","desktop_hardware_id","mobile_hardware_id","user_id") VALUES ('2021-05-22 22:01:23.937','2021-05-22 22:01:23.937',NULL,'61c31ad3-17cc-49a0-b575-565511550f06','21581f9d-7fa1-4119-8749-c72c468af84f',NULL,'b84c8539-9550-4d80-9812-be27806200b5') ON CONFLICT ("id") DO UPDATE SET "user_id"="excluded"."user_id" RETURNING "id"

2021/05/22 22:01:24 C:/Users/sn3ak/go/pkg/mod/github.com/!project!athenaa/database-module@v0.0.0-20210522190032-e0fbf2b16c12/models/user.go:27 SLOW SQL >= 200ms
[841.373ms] [rows:1] INSERT INTO "users" ("id","created_at","updated_at","deleted_at","disabled","encrypted_id") VALUES ('b84c8539-9550-4d80-9812-be27806200b5','2021-05-22 22:01:23.798','2021-05-22 22:01:23.798',NULL,false,'')

2021/05/22 22:01:25 C:/Users/sn3ak/OneDrive/Desktop/Project Athena/services/integration/src/helpers/helpers.go:107 SLOW SQL >= 200ms
[208.761ms] [rows:1] INSERT INTO "stripes" ("created_at","updated_at","deleted_at","customer_id","subscription_id","renewal_date_timestamp","renewal_date","license_id","id") VALUES ('2021-05-22 22:01:24.078','2021-05-22 22:01:24.078',NULL,'cus_XXXXXXXX','sub_XXXXXXXX',1621713683,'2021-05-22 23:01:23.73','1',1) ON CONFLICT ("id") DO UPDATE SET "license_id"="excluded"."license_id" RETURNING "id"

2021/05/22 22:01:25 C:/Users/sn3ak/OneDrive/Desktop/Project Athena/services/integration/src/helpers/helpers.go:107 SLOW SQL >= 200ms
[415.179ms] [rows:1] INSERT INTO "licenses" ("created_at","updated_at","deleted_at","key","desktop_hardware_id","mobile_hardware_id","user_id","id") VALUES ('2021-05-22 22:01:23.937','2021-05-22 22:01:23.937',NULL,'61c31ad3-17cc-49a0-b575-565511550f06','21581f9d-7fa1-4119-8749-c72c468af84f',NULL,'b84c8539-9550-4d80-9812-be27806200b5',1) ON CONFLICT ("id") DO UPDATE SET "user_id"="excluded"."user_id" RETURNING "id"

2021/05/22 22:01:25 C:/Users/sn3ak/OneDrive/Desktop/Project Athena/services/integration/src/helpers/helpers.go:107 SLOW SQL >= 200ms
[974.912ms] [rows:1] UPDATE "users" SET "created_at"='2021-05-22 22:01:23.798',"updated_at"='2021-05-22 22:01:25.049',"deleted_at"=NULL,"disabled"=false,"encrypted_id"='ad2c949bf98b23cdef3828cbc8c31a920000000000000000000000000000000000000000000000000000000000000000' WHERE "id" = 'b84c8539-9550-4d80-9812-be27806200b5'

提前感谢

  • 编辑1
    检索行的代码
var user models.User
//tokens[0]是用户的ID
db.PostgresClient.Where("id = ?", tokens[0]).First(&user)
英文:

I am using gorm with Golang and I have the tables shown below. I am having an issue with retrieving the relation tables, for example when I insert a new user, everything is inserted as shown in the logs below, but as soon as I try to retrieve that user then all the fields that are supposed to be field with foreign tables are either nil or []. From what I can see in the database the values for those tables get inserted and foreign keys are in fact created, so what could be the problem?

Models:

type User struct {
	ID          string `json:"id" gorm:"primaryKey"`
	CreatedAt   time.Time
	UpdatedAt   time.Time
	DeletedAt   gorm.DeletedAt `gorm:"index"`
	License     *License       `json:"license"`
	Statistics  []*Statistic   `json:"statistics"`
	App         *App           `json:"app"`
	Disabled    bool           `json:"disabled"`
	EncryptedID string         `json:"encrypted_id"`
}
//1 of the tables that doesn't get found but exists in database after creation
type License struct {
	gorm.Model
	Key               string  `json:"key"`
	DesktopHardwareID string  `json:"desktop_hardware_id"`
	MobileHardwareID  *string `json:"mobile_hardware_id"`
	Stripe            *Stripe  `json:"stripe" ,gorm:"foreignKey:LicenseID"`
	UserID            string  `json:"user_id"`
}

Logs:

2021/05/22 22:01:24 C:/Users/sn3ak/go/pkg/mod/github.com/!project!athenaa/database-module@v0.0.0-20210522190032-e0fbf2b16c12/models/user.go:27 SLOW SQL >= 200ms
[282.555ms] [rows:1] INSERT INTO "licenses" ("created_at","updated_at","deleted_at","key","desktop_hardware_id","mobile_hardware_id","user_id") VALUES ('2021-05-22 22:01:23.937','2021-05-22 22:01:23.937',NULL,'61c31ad3-17cc-49a0-b575-565511550f06','21581f9d-7fa1-4119-8749-c72c468af84f',NULL,'b84c8539-9550-4d80-9812-be27806200b5') ON CONFLICT ("id") DO UPDATE SET "user_id"="excluded"."user_id" RETURNING "id"

2021/05/22 22:01:24 C:/Users/sn3ak/go/pkg/mod/github.com/!project!athenaa/database-module@v0.0.0-20210522190032-e0fbf2b16c12/models/user.go:27 SLOW SQL >= 200ms
[841.373ms] [rows:1] INSERT INTO "users" ("id","created_at","updated_at","deleted_at","disabled","encrypted_id") VALUES ('b84c8539-9550-4d80-9812-be27806200b5','2021-05-22 22:01:23.798','2021-05-22 22:01:23.798',NULL,false,'')

2021/05/22 22:01:25 C:/Users/sn3ak/OneDrive/Desktop/Project Athena/services/integration/src/helpers/helpers.go:107 SLOW SQL >= 200ms
[208.761ms] [rows:1] INSERT INTO "stripes" ("created_at","updated_at","deleted_at","customer_id","subscription_id","renewal_date_timestamp","renewal_date","license_id","id") VALUES ('2021-05-22 22:01:24.078','2021-05-22 22:01:24.078',NULL,'cus_XXXXXXXX','sub_XXXXXXXX',1621713683,'2021-05-22 23:01:23.73','1',1) ON CONFLICT ("id") DO UPDATE SET "license_id"="excluded"."license_id" RETURNING "id"

2021/05/22 22:01:25 C:/Users/sn3ak/OneDrive/Desktop/Project Athena/services/integration/src/helpers/helpers.go:107 SLOW SQL >= 200ms
[415.179ms] [rows:1] INSERT INTO "licenses" ("created_at","updated_at","deleted_at","key","desktop_hardware_id","mobile_hardware_id","user_id","id") VALUES ('2021-05-22 22:01:23.937','2021-05-22 22:01:23.937',NULL,'61c31ad3-17cc-49a0-b575-565511550f06','21581f9d-7fa1-4119-8749-c72c468af84f',NULL,'b84c8539-9550-4d80-9812-be27806200b5',1) ON CONFLICT ("id") DO UPDATE SET "user_id"="excluded"."user_id" RETURNING "id"

2021/05/22 22:01:25 C:/Users/sn3ak/OneDrive/Desktop/Project Athena/services/integration/src/helpers/helpers.go:107 SLOW SQL >= 200ms
[974.912ms] [rows:1] UPDATE "users" SET "created_at"='2021-05-22 22:01:23.798',"updated_at"='2021-05-22 22:01:25.049',"deleted_at"=NULL,"disabled"=false,"encrypted_id"='ad2c949bf98b23cdef3828cbc8c31a920000000000000000000000000000000000000000000000000000000000000000' WHERE "id" = 'b84c8539-9550-4d80-9812-be27806200b5'

Thanks in advance

  • EDIT 1
    Code that retrieves the rows
var user models.User
//tokens[0] is the ID of the user
db.PostgresClient.Where("id = ?", tokens[0]).First(&user)

答案1

得分: 0

要加载关系表中的数据,您可以使用Preload函数。

要加载所有一级关联,您可以这样做:

db.PostgresClient.Preload(clause.Associations).Where("id = ?", tokens[0]).First(&user)

但是,对于嵌套关联,您需要逐个加载它们:

db.PostgresClient.Preload("License.Stripe").Where("id = ?", tokens[0]).First(&user)

您可以查看文档获取更多详细信息。

英文:

To load the data from the relation tables, you can use the Preload function.

To load all first-level associations, you can do something like this:

db.PostgresClient.Preload(clause.Associations).Where("id = ?", tokens[0]).First(&user)

but, for nested associations, you need to do them individually:

db.PostgresClient.Preload("License.Stripe").Where("id = ?", tokens[0]).First(&user)

You can check the documentation for more details.

huangapple
  • 本文由 发表于 2021年5月23日 03:19:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/67653358.html
匿名

发表评论

匿名网友

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

确定