GORM Golang操作MySQL数据库 数据类型对应关系和表属性

package main

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"log"
)

var db *gorm.DB

func main() {
	// 设置数据库
	const (
		UserName     = "UserName"
		Password     = "Password"
		URL          = "127.0.0.1:3306"
		DatabaseName = "testgorm"
	)
	dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",
		UserName, Password, URL, DatabaseName) // DSN data source name
	var err error
	db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		log.Fatalln(err)
	}

	type Node struct {
		gorm.Model
		Bool    bool
		Int     int
		Uint    uint
		Int8    int8
		Uint8   uint8
		Byte    byte
		Int32   int32
		Rune    rune
		Uint32  uint32
		Int64   int64
		Uint64  uint64
		Float32 float32
		Float64 float64
		String  string
	}

	// 创建表时添加后缀
	// 默认值:ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
	err = db.Set("gorm:table_options",
		"ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin ROW_FORMAT=Dynamic").
		AutoMigrate(&Node{})
}

实际执行的操作

CREATE TABLE `nodes` (
`id` bigint unsigned AUTO_INCREMENT,
`created_at` datetime(3) NULL,
`updated_at` datetime(3) NULL,
`deleted_at` datetime(3) NULL,
`bool` boolean,`int` bigint,
`uint` bigint unsigned,
`int8` tinyint,
`uint8` tinyint unsigned,
`byte` tinyint unsigned,
`int32` int,`rune` int,
`uint32` int unsigned,
`int64` bigint,
`uint64` bigint unsigned,
`float32` float,
`float64` double,
`string` longtext,
PRIMARY KEY (`id`),
INDEX idx_nodes_deleted_at (`deleted_at`)
)ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin ROW_FORMAT=Dynamic
mysql> show create table nodes;
+-------+-----------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table




                                                                                                                             |
+-------+-----------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------+
| nodes | CREATE TABLE `nodes` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `created_at` datetime(3) DEFAULT NULL,
  `updated_at` datetime(3) DEFAULT NULL,
  `deleted_at` datetime(3) DEFAULT NULL,
  `bool` tinyint(1) DEFAULT NULL,
  `int` bigint DEFAULT NULL,
  `uint` bigint unsigned DEFAULT NULL,
  `int8` tinyint DEFAULT NULL,
  `uint8` tinyint unsigned DEFAULT NULL,
  `byte` tinyint unsigned DEFAULT NULL,
  `int32` int DEFAULT NULL,
  `rune` int DEFAULT NULL,
  `uint32` int unsigned DEFAULT NULL,
  `int64` bigint DEFAULT NULL,
  `uint64` bigint unsigned DEFAULT NULL,
  `float32` float DEFAULT NULL,
  `float64` double DEFAULT NULL,
  `string` longtext COLLATE utf8_bin,
  PRIMARY KEY (`id`),
  KEY `idx_nodes_deleted_at` (`deleted_at`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin ROW_FORMAT=DYNAMIC |
+-------+-----------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.02 sec)

mysql> desc nodes;
+------------+------------------+------+-----+---------+----------------+
| Field      | Type             | Null | Key | Default | Extra          |
+------------+------------------+------+-----+---------+----------------+
| id         | bigint unsigned  | NO   | PRI | NULL    | auto_increment |
| created_at | datetime(3)      | YES  |     | NULL    |                |
| updated_at | datetime(3)      | YES  |     | NULL    |                |
| deleted_at | datetime(3)      | YES  | MUL | NULL    |                |
| bool       | tinyint(1)       | YES  |     | NULL    |                |
| int        | bigint           | YES  |     | NULL    |                |
| uint       | bigint unsigned  | YES  |     | NULL    |                |
| int8       | tinyint          | YES  |     | NULL    |                |
| uint8      | tinyint unsigned | YES  |     | NULL    |                |
| byte       | tinyint unsigned | YES  |     | NULL    |                |
| int32      | int              | YES  |     | NULL    |                |
| rune       | int              | YES  |     | NULL    |                |
| uint32     | int unsigned     | YES  |     | NULL    |                |
| int64      | bigint           | YES  |     | NULL    |                |
| uint64     | bigint unsigned  | YES  |     | NULL    |                |
| float32    | float            | YES  |     | NULL    |                |
| float64    | double           | YES  |     | NULL    |                |
| string     | longtext         | YES  |     | NULL    |                |
+------------+------------------+------+-----+---------+----------------+
18 rows in set (0.04 sec)

mysql>

参考资料:
迁移 | GORM - The fantastic ORM library for Golang, aims to be developer friendly.
创建表时附带的ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic 的解释_atu1111的博客-CSDN博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值