MCPcopy
hub / github.com/go-gorm/gorm / TestCreateFromSubQuery

Function TestCreateFromSubQuery

tests/create_test.go:523–544  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

521}
522
523func TestCreateFromSubQuery(t *testing.T) {
524 user := User{Name: "jinzhu"}
525
526 DB.Create(&user)
527
528 subQuery := DB.Table("users").Where("name=?", user.Name).Select("id")
529
530 result := DB.Session(&gorm.Session{DryRun: true}).Model(&Pet{}).Create([]map[string]interface{}{
531 {
532 "name": "cat",
533 "user_id": gorm.Expr("(?)", DB.Table("(?) as tmp", subQuery).Select("@uid:=id")),
534 },
535 {
536 "name": "dog",
537 "user_id": gorm.Expr("@uid"),
538 },
539 })
540
541 if !regexp.MustCompile(`INSERT INTO .pets. \(.name.,.user_id.\) .*VALUES \(.+,\(SELECT @uid:=id FROM \(SELECT id FROM .users. WHERE name=.+\) as tmp\)\),\(.+,@uid\)`).MatchString(result.Statement.SQL.String()) {
542 t.Errorf("invalid insert SQL, got %v", result.Statement.SQL.String())
543 }
544}
545
546func TestCreateNilPointer(t *testing.T) {
547 var user *User

Callers

nothing calls this directly

Calls 8

ExprFunction · 0.92
ModelMethod · 0.80
SessionMethod · 0.80
CreateMethod · 0.65
SelectMethod · 0.65
WhereMethod · 0.65
TableMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected