- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何保存用户与 Gorm 和 Postgres 的地址关系?
package main
import (
"fmt"
"log"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
)
var (
pgUri = "postgres://postgres@127.0.0.1:5432/postgres?sslmode=disable"
)
type User struct {
gorm.Model
Email string
Address Address
}
type Address struct {
gorm.Model
Street string
City string
Country string
}
func main() {
db, err := gorm.Open("postgres", pgUri)
if err != nil {
log.Fatalf("Failed to connect Postgres: %v\n", err)
}
// Checked two tables (user, address) created in database
db.AutoMigrate(&User{}, &Address{})
defer db.Close()
u := User{
Email: "some@one.com",
Address: Address{
Street: "One street",
City: "Two city",
Country: "Three country",
},
}
fmt.Println(u)
if err := db.Create(&u).Error; err != nil {
panic(err)
}
if err := db.Save(&u).Error; err != nil {
panic(err)
}
}
在我使用 go run main.go
运行它之后:
{{0 0001-01-01 00:00:00 +0000 UTC 0001-01-01 00:00:00 +0000 UTC <nil>} some@one.com {{0 0001-01-01 00:00:00 +0000 UTC 0001-01-01 00:00:00 +0000 UTC <nil>} One street Two city Three country}}
它会创建一个新用户,但不会创建任何地址。
最佳答案
您在 Address
关联中缺少外键。对于 has one 关系,外键字段必须存在,owned会将属于它的模型的主键保存到这个字段中。
type User struct {
gorm.Model
Email string
Address Address // One-To-One relationship (has one - use Address's UserID as foreign key)
}
type Address struct {
gorm.Model
UserID uint
Street string
City string
Country string
}
关于postgresql - 如何保存与 Gorm 的一对一关系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59713423/
我在使用 Go 进行应用程序开发,我使用 gorm 库连接到数据库。我看到 gorm 有 2 个库 github.com/jinzhu/gorm和 gorm.io/gorm . 我不知道该用哪个。它们
我是 Gorm 的新手。我正在尝试进行级联删除,如果我删除一个用户,则与该用户关联的角色(属于)、个人资料(有一个)和书籍(一对多)也将被删除。 我在下面设置了我的模型,但级联似乎不起作用。当我删除我
使用 Jinzhu 的 GORM 包,顺便说一句,我现在有这个结构: type User struct { gorm.Model // The Users username U
Golang 的 GORM 库支持复合主键。但是如何从相关模型中引用它们呢? 例如,假设我有一个 User 和一个 Note 模型: type User struct { Organizati
我正在尝试使用 Grails 项目作为插件,基本上将我的域类放在插件中,然后在多个 Grails 项目中使用它们。 我已经这样做了: grails 创建应用程序网页 grails 创建应用程序插件 在
Golang 的 GORM 库支持复合主键。但是如何从相关模型中引用它们呢? 例如,假设我有一个 User 和一个 Note 模型: type User struct { Organizati
如何省略来自 gorm.Model 的列。例如。创建日期、修改日期。 考虑以下模型: type User struct{ gorm.Model Firstname string
当我在 Grails 中执行 domainObj1 == domainObj2 时,对象是按 ID 进行比较的吗?如果不是,如何比较? 最佳答案 首先,您需要了解 GORM/Grails 在 equa
目前正在为此苦苦挣扎。 我希望能够使用抽象域类来使我能够使用一些通用代码来执行一些常用操作。 我的问题是很多 GORM 操作都是域类上的静态方法,这使得它变得困难。想知道这些方法是否有任何非静态等价物
我有一个结构/模型 type User struct { gorm.Model Name string `gorm:"unique;not null"
我想创建一组在我的应用程序中使用的 gorm 类型。所以我想用我的类型定义一个 map gorm.DB作为键和空的结构{}作为标志: var ( autoMigrations map[gorm
有表 customer_account (postgres) 是从 YII2 迁移过来的。 数据链接: CREATE TABLE public.test_table ( id INTEGER PR
我有一个域对象,它已经有一个名为 versions 的属性,所以我想给内置的 version 属性一个不同的名称(在 GORM 中用于乐观锁定)。例如,我想将其称为 updateCount。 请注意,
我有一个普通的 Grails 3.3.2 应用程序。我可以使用 gradle testintegrationTest 很好地运行测试。但是,当我尝试右键单击并在 IntelliJ 中运行测试类或单个测
除了这样做对性能的明显影响之外,还有什么好的技术原因 不是 设置 grails.gorm.autoFlush = true和 grails.gorm.failOnError = true在 Confi
GORM 似乎不尊重我基于外键关联表的尝试,而我的主键关联有效。 我有 3 个结构,它们与 3 个单独的数据库表相关联。它们的关系如下: Inventory.CustID 与 Customer.Cus
我有一个像这样的 golang 结构的表: Order { ID TransactionID Transaction } Transaction { ID ProfileID
我正在使用 Gorm,对如何从模型中检索嵌套的 SubComments 有一些疑问。我遇到的问题是评论嵌套了两层深,即 Comment.SubComments 没有加载。我是否遗漏了 Preload
我正在尝试使用 golang gorm 创建一个自我引用的一对多关系。每个用户可以创建多个其他用户 (Created),但一个用户始终由另一个用户创建 (UserID)。 type User stru
假设我有 2 个表,它们共享一些列名,例如: table_1 - id - created_at - deleted_at - name - color table_2 - id - created_
我是一名优秀的程序员,十分优秀!