小编ecl*_*ude的帖子

Golang GORM使用Lat和Lng查询位置

我正在尝试用GORM提出具体要求.

这是我使用的表格:

+-------------------+
| Tables            |
+-------------------+
| locations         |
| shops             |
| shops_tags        |
| tags              |
+-------------------+
Run Code Online (Sandbox Code Playgroud)

地点表

+------------+------------------+------+-----+---------+----------------+
| Field      | Type             | Null | Key | Default | Extra          |
+------------+------------------+------+-----+---------+----------------+
| id         | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| shop_id    | bigint(20)       | YES  |     | NULL    |                |
| lat        | decimal(10,8)    | YES  |     | NULL    |                |
| lng        | decimal(11,8)    | YES …
Run Code Online (Sandbox Code Playgroud)

mysql go go-gorm

8
推荐指数
1
解决办法
458
查看次数

访问many2many表数据

我想处理many2many由 GORM 自动生成的表db.AutoMigrate(&Document{}, &Folder{})

在这种情况下,文件夹有文档,文档有文件夹,到目前为止还不错,但是关联有状态(有效、无效...),所以我必须查看documents_folders表格才能知道状态。

数据库:

+-----------------------+
| Table                 |
+-----------------------+
| documents             |
| documents_folders     |
| folders               |
+-----------------------+
Run Code Online (Sandbox Code Playgroud)

楷模 :

东西

(ID primary_key == UUID)

type Stuff struct {
    ID          uuid.UUID  `gorm:"type:char(36);primary_key;" json:"id"`
    CreatedAt   time.Time  `json:"created_at"`
    UpdatedAt   time.Time  `json:"update_at"`
    DeletedAt   *time.Time `sql:"index" json:"deleted_at"`
}
Run Code Online (Sandbox Code Playgroud)

文件夹

type Folder struct {
    Stuff
    Name      string      `json:"name" form:"name" gorm:"name;"`
    Documents []*Document `json:"documents" gorm:"many2many:documents_folders;"`
}
Run Code Online (Sandbox Code Playgroud)

文档

type Document struct {
    Stuff
    Name        string    `json:"name" form:"name" gorm:"name;"` …
Run Code Online (Sandbox Code Playgroud)

go go-gorm

5
推荐指数
1
解决办法
290
查看次数

标签 统计

go ×2

go-gorm ×2

mysql ×1