Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Demonstrate bad Where clause #661

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,37 @@ package main

import (
"testing"

"github.com/gofrs/uuid"
)

// GORM_REPO: https://github.com/go-gorm/gorm.git
// GORM_BRANCH: master
// TEST_DRIVERS: sqlite, mysql, postgres, sqlserver

func TestGORM(t *testing.T) {
user := User{Name: "jinzhu"}
uuid1, _ := uuid.NewV4()
user := User{Name: "jinzhu", UserID: uuid1}
uuid2, _ := uuid.NewV4()
user2 := User{Name: "philicious", UserID: uuid2}

DB.Create(&user)
DB.Create(&user2)

var resultGood []User
_ = DB.Debug().Where(&User{UserID: uuid1}).Find(&resultGood)
// resulting query: SELECT * FROM `users` WHERE `users`.`user_id` = "a543742b-9fe4-49ec-9226-e980be5669a8" AND `users`.`deleted_at` IS NULL

if len(resultGood) > 1 {
t.Errorf("should only find a single user")
}

var resultBad []User
uuidNull, _ := uuid.FromString("00000000-0000-0000-0000-000000000000") // same as using uuid.Nil or uuid.FromStringOrNil(uuidString)
_ = DB.Debug().Where(&User{UserID: uuidNull}).Find(&resultBad)
// resulting query: SELECT * FROM `users` WHERE `users`.`deleted_at` IS NULL

var result User
if err := DB.First(&result, user.ID).Error; err != nil {
t.Errorf("Failed, got error: %v", err)
if len(resultBad) > 0 {
t.Errorf("should NOT find a user")
}
}
2 changes: 2 additions & 0 deletions models.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"database/sql"
"time"

"github.com/gofrs/uuid"
"gorm.io/gorm"
)

Expand All @@ -13,6 +14,7 @@ import (
// His pet also has one Toy (has one - polymorphic)
type User struct {
gorm.Model
UserID uuid.UUID
Name string
Age uint
Birthday *time.Time
Expand Down
Loading