How to access gorm.Model.ID? - TagMerge
6How to access gorm.Model.ID?How to access gorm.Model.ID?

How to access gorm.Model.ID?

Asked 5 months ago
7
6 answers

You're very close in your last example. You can remove the gorm.Model inheritance from your struct if you want/need (I personally do that for clarity), but to access that value you'd just need to build up your struct a little more. For example...

type Address struct {
    gorm.Model
    Name         string
    Email        string  `gorm:"type:varchar(100);unique_index"`
    Role         string  `gorm:"size:255"` // set field size to 255
}

c.Ctx.DB.Delete(&models.Address{gorm.Model: gorm.Model{ID: id}})

Give that a try and see if that works for you. Alternatively revert to your method without inheriting the gorm.Model

Source: link

6

I worked with this: removing package Name 'gorm'

c.Ctx.DB.Delete(&models.Address{Model: gorm.Model{ID: id}})

Source: link

0

looks like this is an interesting question - but of category 'selfmade problem':

what about

c.Ctx.DB.Delete(&model.Address{}, id)

then you can keep the benefits (or not - depends on taste) of gorm.Model

Source: link

0

type User struct {  ID           uint  Name         string  Email        *string  Age          uint8  Birthday     *time.Time  MemberNumber sql.NullString  ActivatedAt  sql.NullTime  CreatedAt    time.Time  UpdatedAt    time.Time}
// gorm.Model definitiontype Model struct {  ID        uint           `gorm:"primaryKey"`  CreatedAt time.Time  UpdatedAt time.Time  DeletedAt gorm.DeletedAt `gorm:"index"`}
type User struct {  Name string `gorm:"<-:create"` // allow read and create  Name string `gorm:"<-:update"` // allow read and update  Name string `gorm:"<-"`        // allow read and write (create and update)  Name string `gorm:"<-:false"`  // allow read, disable write permission  Name string `gorm:"->"`        // readonly (disable write permission unless it configured)  Name string `gorm:"->;<-:create"` // allow read and create  Name string `gorm:"->:false;<-:create"` // createonly (disabled read from db)  Name string `gorm:"-"`            // ignore this field when write and read with struct  Name string `gorm:"-:all"`        // ignore this field when write, read and migrate with struct  Name string `gorm:"-:migration"`  // ignore this field when migrate with struct}
type User struct {  CreatedAt time.Time // Set to current time if it is zero on creating  UpdatedAt int       // Set to current unix seconds on updating or if it is zero on creating  Updated   int64 `gorm:"autoUpdateTime:nano"` // Use unix nano seconds as updating time  Updated   int64 `gorm:"autoUpdateTime:milli"`// Use unix milli seconds as updating time  Created   int64 `gorm:"autoCreateTime"`      // Use unix seconds as creating time}
type User struct {  gorm.Model  Name string}// equalstype User struct {  ID        uint           `gorm:"primaryKey"`  CreatedAt time.Time  UpdatedAt time.Time  DeletedAt gorm.DeletedAt `gorm:"index"`  Name string}

Source: link

0

import (  "gorm.io/driver/mysql"  "gorm.io/gorm")func main() {  // refer https://github.com/go-sql-driver/mysql#dsn-data-source-name for details  dsn := "user:pass@tcp(127.0.0.1:3306)/dbname?charset=utf8mb4&parseTime=True&loc=Local"  db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})}
db, err := gorm.Open(mysql.New(mysql.Config{  DSN: "gorm:gorm@tcp(127.0.0.1:3306)/gorm?charset=utf8&parseTime=True&loc=Local", // data source name  DefaultStringSize: 256, // default size for string fields  DisableDatetimePrecision: true, // disable datetime precision, which not supported before MySQL 5.6  DontSupportRenameIndex: true, // drop & create when rename index, rename index not supported before MySQL 5.7, MariaDB  DontSupportRenameColumn: true, // `change` when rename column, rename column not supported before MySQL 8, MariaDB  SkipInitializeWithVersion: false, // auto configure based on currently MySQL version}), &gorm.Config{})
import (  _ "example.com/my_mysql_driver"  "gorm.io/gorm")db, err := gorm.Open(mysql.New(mysql.Config{  DriverName: "my_mysql_driver",  DSN: "gorm:gorm@tcp(localhost:9910)/gorm?charset=utf8&parseTime=True&loc=Local", // data source name, refer https://github.com/go-sql-driver/mysql#dsn-data-source-name}), &gorm.Config{})
import (  "database/sql"  "gorm.io/driver/mysql"  "gorm.io/gorm")sqlDB, err := sql.Open("mysql", "mydb_dsn")gormDB, err := gorm.Open(mysql.New(mysql.Config{  Conn: sqlDB,}), &gorm.Config{})
import (  "gorm.io/driver/postgres"  "gorm.io/gorm")dsn := "host=localhost user=gorm password=gorm dbname=gorm port=9920 sslmode=disable TimeZone=Asia/Shanghai"db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})

Source: link

0

0
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
package database
import "github.com/jinzhu/gorm"
var (

	DBCon *gorm.DB


)
func InitDB() {
	var err error

	//connect to postgres database
	DBCon, err = gorm.Open("postgres", "host=localhost port=myport user=gorm dbname=delivery password=mypassword")

	//where myhost is port is the port postgres is running on
	//user is your postgres use name
	//password is your postgres password
	if err != nil {

		panic("failed to connect database")
	}

}
0
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
package main
import (
	"food-delivery/database"

	_ "github.com/jinzhu/gorm/dialects/postgres"

)
func main() {

	////initialize the database
	database.InitDB()


	///finally close the connection when you are done
	defer database.DBCon.Close()


}
0
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

Source: link

Recent Questions on go

    Programming Languages