DB Query Commands are producing strange sql queries

Viewed 1018

I have my model as:

type Report struct {
    ID    int     `json:"id,omitempty" gorm:"primary_key"`
    Title *string `json:"title" gorm:"not null"`
}

I have initialized variable report as var report Report I have successfully auto migrated this model as database table and have populated database as sql INSERT using GORM's db.Create(&report).

The problem I am facing is while trying query commands. Every query commands supported by GORM such as db.Find(&report) , db.First(&report, 1) is resulting to queries such as folows:

SELECT * FROM "reports"  WHERE "reports"."deleted_at" IS NULL AND ((id = $1))
SELECT * FROM "reports"  WHERE "reports"."deleted_at" IS NULL AND ((id = $1))
SELECT * FROM reports WHERE (reports.deleted_at IS NULL) AND ((id = $1))
SELECT * FROM reports WHERE (reports.deleted_at IS NULL) AND ((id = $1))
SELECT 0 done

I am unable to query database. I am using GORM with cockroach db. This works fine when using GO pq driver and raw sql commands.

2 Answers

Since the model has the field deleted_at, gorm is using the soft delete ability automatically. You can use Unscoped

 db.Unscoped().Find(&reports)

Which is the same as running the raw query

db.Raw("SELECT * FROM reports").Scan(&reports)
Related