如何正确使用 FirstOrCreate
How to use FirstOrCreate properly
我有以下简单结构
type Profile struct {
gorm.Model
Email string `json:"email" sql:"not null;unique"`
LastLogin time.Time `json:"lastlogin"`
}
如果它不存在,我正在尝试通过
插入
db.Con.Debug().Where(db.Profile{Email: "user@domain.com"}).Assign(db.Profile{LastLogin: time.Now()}).FirstOrCreate(&profile)
我在日志中得到以下内容
(/Users/mzupan/go/src/gitlab.com/org/app/pkg/auth/login.go:182)
[2018-09-24 13:35:58] [4.56ms] SELECT * FROM "profiles" WHERE "profiles"."deleted_at" IS NULL AND (("profiles"."email" = 'user@domain.com')) ORDER BY "profiles"."id" ASC LIMIT 1
[0 rows affected or returned ]
(/Users/mzupan/go/src/gitlab.com/org/app/pkg/auth/login.go:182)
[2018-09-24 13:35:58] [1.77ms] UPDATE "profiles" SET "last_login" = '2018-09-24 13:35:58', "updated_at" = '2018-09-24 13:35:58' WHERE "profiles"."deleted_at" IS NULL AND (("profiles"."email" = 'user@domain.com'))
[0 rows affected or returned ]
所以即使在 select 中找到 0 行,它也会尝试执行 select/update。在我看来,我正在做正确的事。
我想你忘了创建 table db.CreateTable(&Profile{})
这是一个工作示例:
package main
import (
"time"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
)
type Profile struct {
gorm.Model
Email string `json:"email" sql:"not null;unique"`
LastLogin time.Time `json:"lastlogin"`
}
func main() {
db, err := gorm.Open("sqlite3", "test.db")
if err != nil {
panic("failed to connect database")
}
defer db.Close()
// Create the table...
// Read the doc at: http://doc.gorm.io/database.html#migration -> sec: Create Table
db.CreateTable(&Profile{})
var profile Profile
db.Debug().Where(Profile{Email: "user@domain.com"}).Assign(Profile{LastLogin: time.Now()}).FirstOrCreate(&profile)
}
输出:
[2018-09-25 09:47:35] [0.18ms] INSERT INTO "profiles" ("created_at","updated_at","deleted_at","email","last_login") VALUES ('2018-09-25 09:47:35','2018-09-25 09:47:35',NULL,'user@domain.com','2018-09-25 09:47:35')
[1 rows affected or returned ]
希望对您有所帮助:)
我有以下简单结构
type Profile struct {
gorm.Model
Email string `json:"email" sql:"not null;unique"`
LastLogin time.Time `json:"lastlogin"`
}
如果它不存在,我正在尝试通过
插入 db.Con.Debug().Where(db.Profile{Email: "user@domain.com"}).Assign(db.Profile{LastLogin: time.Now()}).FirstOrCreate(&profile)
我在日志中得到以下内容
(/Users/mzupan/go/src/gitlab.com/org/app/pkg/auth/login.go:182)
[2018-09-24 13:35:58] [4.56ms] SELECT * FROM "profiles" WHERE "profiles"."deleted_at" IS NULL AND (("profiles"."email" = 'user@domain.com')) ORDER BY "profiles"."id" ASC LIMIT 1
[0 rows affected or returned ]
(/Users/mzupan/go/src/gitlab.com/org/app/pkg/auth/login.go:182)
[2018-09-24 13:35:58] [1.77ms] UPDATE "profiles" SET "last_login" = '2018-09-24 13:35:58', "updated_at" = '2018-09-24 13:35:58' WHERE "profiles"."deleted_at" IS NULL AND (("profiles"."email" = 'user@domain.com'))
[0 rows affected or returned ]
所以即使在 select 中找到 0 行,它也会尝试执行 select/update。在我看来,我正在做正确的事。
我想你忘了创建 table db.CreateTable(&Profile{})
这是一个工作示例:
package main
import (
"time"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
)
type Profile struct {
gorm.Model
Email string `json:"email" sql:"not null;unique"`
LastLogin time.Time `json:"lastlogin"`
}
func main() {
db, err := gorm.Open("sqlite3", "test.db")
if err != nil {
panic("failed to connect database")
}
defer db.Close()
// Create the table...
// Read the doc at: http://doc.gorm.io/database.html#migration -> sec: Create Table
db.CreateTable(&Profile{})
var profile Profile
db.Debug().Where(Profile{Email: "user@domain.com"}).Assign(Profile{LastLogin: time.Now()}).FirstOrCreate(&profile)
}
输出:
[2018-09-25 09:47:35] [0.18ms] INSERT INTO "profiles" ("created_at","updated_at","deleted_at","email","last_login") VALUES ('2018-09-25 09:47:35','2018-09-25 09:47:35',NULL,'user@domain.com','2018-09-25 09:47:35')
[1 rows affected or returned ]
希望对您有所帮助:)