包含标签 gorm 中的文章

Gorm: 声明模型(1)

Gorm: 声明模型(1) 建议点击 查看原文 查看最新内容。 原文链接: https://typonotes.com/posts/2025/03/17/gorm-model-declaration/ 1. 数组字段 https://gorm.io/zh_CN/docs/models.html 如果表中有 slice 字段, 则需要使用 type 指定类型。 1 2 3 4 5 6 package dao type DemoTable struct { Users []string `gorm:"type:text[]"` // This is a slice of strings IDs []int `gorm:"type:int[]"` // This is a slice of integers } 2. 索引 https://gorm.io/zh_CN/docs/indexes.html 2.1 唯一作引 唯一索引有两种形式, uniqueIndex index:[name],unique : 自定义索引名字 1 2 3 4 5 type User struct { Name string `gorm:"index"` // 索引 Name4 string `gorm:"uniqueIndex"` // 唯一索引……

阅读全文

gorm 数据库表模型声明 - 基础

gorm 数据库表模型声明 - 基础 链接数据库 1 2 3 4 5 6 7 8 9 10 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{}) } 常用字段类型与 gorm 默认字段类型 varchar, int, datetime, timestamp 表定义如下 1 2 3 4 5 type Author struct { gorm.Model Name string Password string } auto migrate 后, 可以看到 name, password 默认使用的是 longtext 类型。 1 2 3 4 5 6 7 8 9 10 11 12 show create table authors; CREATE TABLE `authors` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `created_at` datetime(3) DEFAULT NULL, `updated_at` datetime(3) DEFAULT……

阅读全文

福利派送

  • (免费星球)「运维成长路线」

  • 又拍云免费 CDN

最近文章

分类

标签

其它