Questions tagged [go-gorm]

GORM is an ORM library for the programming language Go, developed by jinzhu.

GORM is an ORM library for the programming language Go, developed by jinzhu.

It is licensed under MIT license.

See also

1820 questions
55
votes
3 answers

golang gorm Access the underlying mysql query

Is there a way to get the sql query log from https://github.com/jinzhu/gorm? e.g. in dev environment, it would be useful to be able to log to the console the mysql queries that have been called. e.g. how to get the underlying sql query log for the…
Gravy
  • 12,264
  • 26
  • 124
  • 193
40
votes
3 answers

reflect.Value.Set using unaddressable value

g.GET("/", func(c echo.Context) error { var users []models.User err := db.Find(users).Error if err != nil { fmt.Println(err) } return c.JSON(http.StatusOK, users) }) this is the code for getting and displaying users from…
Krishna Satya
  • 775
  • 2
  • 9
  • 21
37
votes
3 answers

How to set singular name for a table in gorm

type user struct { ID int Username string `gorm:"size:255"` Name string `gorm:"size:255"` } I want to create a table 'user' using this model. But the table name is automatically set to 'users'. I know it is gorm's default…
Yash Goel
  • 530
  • 1
  • 6
  • 15
33
votes
6 answers

Gorm Golang orm associations

I'm using Go with the GORM ORM. I have the following structs. The relation is simple. One Town has multiple Places and one Place belongs to one Town. type Place struct { ID int Name string Town Town } type Town struct…
Javier Cadiz
  • 12,326
  • 11
  • 55
  • 76
26
votes
10 answers

How to Create or Update a record with GORM?

Gorm has a FirstOrCreate method and a FirstOrInit but how to check afterwards if the record was actually created? I like to create a record if it does not exists and if it exists I want to update some fields.
Tarion
  • 16,283
  • 13
  • 71
  • 107
26
votes
8 answers

How do you do UUID in Golangs Gorm?

I have the following model... type User struct { ID string `sql:"type:uuid;primary_key;default:uuid_generate_v4()"` FirstName string `form:"first_name" json:"first_name,omitempty"` LastName string `form:"last_name"…
Ewan Valentine
  • 3,741
  • 7
  • 43
  • 68
25
votes
3 answers

How can I check for errors in CRUD operations using GORM?

The official documentation for GORM demonstrates a way in which one can test for the existence of a record, i.e.: user := User{Name: "Jinzhu", Age: 18, Birthday: time.Now()} // returns true if record hasn’t been saved (primary key `Id` is…
Louis Thibault
  • 20,240
  • 25
  • 83
  • 152
23
votes
3 answers

sqlmock is not matching query, but query is identical and log output shows the same

I'm trying to write tests for some code using Gorm using sqlmock. I figured out writing tests for my insert function but now pulling my hair out trying to get an update working. First piece of the workflow merely queries the record from the…
user11471017
22
votes
7 answers

GORM doesnt update boolean field to false

On updates gorm doesnt update boolean type to false. By default it updates to true, but when i try to update to false not changes. I dont see any errors also. What can be the issue ? type Attendee struct { ID uint …
7urkm3n
  • 6,054
  • 4
  • 29
  • 46
20
votes
1 answer

Golang Gorm one-to-many with has-one

I'm trying to learn Go and Gorm by building a little prototype order management app. The database is MySQL. With simple queries Gorm has been stellar. However, when trying to obtain a result set involving a combination one-to-many with a has-one…
Michael Babcock
  • 703
  • 2
  • 7
  • 13
19
votes
1 answer

What does Preload function do in gorm?

The link http://gorm.io/docs/preload.html talks about preloading in GORM, but I am unable to understand what this function does. type User struct { gorm.Model Username string Orders Order } type Order struct { gorm.Model UserID uint …
Harsh Agarwal
  • 675
  • 2
  • 13
  • 28
18
votes
6 answers

Inserting and selecting PostGIS Geometry with Gorm

I've been trying to find a way to insert and retrieve geometric types using Golang, and specifically the library gorm. I'm also attempting to use the library orb that defines different types for geometries, and provides encoding/decoding between…
robbieperry22
  • 1,753
  • 1
  • 18
  • 49
18
votes
2 answers

GORM Not ignoring field with `gorm:"-"`

Using Jinzhu's GORM Package which is fantastic btw, I currently have this struct: type User struct { gorm.Model // The Users username Username string `gorm:"size:255;unique;not null"` // The Users email address Email string…
Datsik
  • 14,453
  • 14
  • 80
  • 121
17
votes
5 answers

How to create a Postgres database using GORM

This is primarily focused towards having setup() and teardown() methods for a test suite that I'm planning on writing that involves creation of a DB. I've figured out how to create a DB using GORM. However, I'm not sure if this is the best…
Aditya Satyavada
  • 1,028
  • 1
  • 10
  • 14
16
votes
2 answers

Unable to use type string as sql.NullString

I am creating a gorm model // Day is a corresponding day entry type Day struct { gorm.Model Dateday string `json:"dateday" gorm:"type:date;NOT NULL"` Nameday string `json:"nameday" gorm:"type:varchar(100);NOT NULL"` …
pkaramol
  • 16,451
  • 43
  • 149
  • 324
1
2 3
99 100