Golang Gorm不使用约束条件创建表

huangapple go评论80阅读模式
英文:

Golang Gorm not creating table with constraints

问题

我正在使用Gorm开发一个Gin应用程序。目前,我有以下表示模型的结构体:

// Category represents a category object in the database
type Category struct {
    Name        string `json:"name" gorm:"size:60;unique;not null"`
    Description string `json:"description" gorm:"size:120"`
    Parent      uint   `json:"parent"`
    Active      bool   `json:"active" gorm:"default:true"`
    gorm.Model
}

正如你所看到的,有一些约束条件,比如sizeuniquenot null

当我运行迁移命令db.AutoMigrate(&entities.Category{})时,表确实被创建了,但并没有按照指定的约束条件创建。

检查表的DDL语句,它是这样创建的:

CREATE TABLE `categories` (
  `name` longtext DEFAULT NULL,
  `description` varchar(120) DEFAULT NULL,
  `parent` int(10) unsigned DEFAULT NULL,
  `active` tinyint(1) DEFAULT 1,
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL,
  `deleted_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_categories_deleted_at` (`deleted_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

你知道我做错了什么吗?

英文:

I'm working on a Gin app with Gorm. Currently, I've got the following struct which represents a model:

// Category represents a category object in the database
type Category struct {
	Name        string `json:"name" gorm:"size:60,unique,not null"`
	Description string `json:"description" gorm:"size:120"`
	Parent      uint   `json:"parent"`
	Active      bool   `json:"active" gorm:"default:true"`
	gorm.Model
}

as you can see, there are some constraints such as size, unique, and not null.

When I run the migration
db.AutoMigrate(&entities.Category{})

the table is in fact created, but not with the specified constraints.
Inspecting the table's DDL, here's how it is being created:

CREATE TABLE `categories` (
  `name` longtext DEFAULT NULL,
  `description` varchar(120) DEFAULT NULL,
  `parent` int(10) unsigned DEFAULT NULL,
  `active` tinyint(1) DEFAULT 1,
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL,
  `deleted_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_categories_deleted_at` (`deleted_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

any idea what am I doing wrong?

答案1

得分: 2

根据文档,我认为在标签约束声明之间应该使用分号(;)而不是逗号(,)。

type Category struct {
    Name        string `json:"name" gorm:"size:60;unique;not null"`
    Description string `json:"description" gorm:"size:120"`
    Parent      uint   `json:"parent"`
    Active      bool   `json:"active" gorm:"default:true"`
    gorm.Model
}
英文:

Based on the doc, I believe you should use semicolon (;) instead comma (,) between tag constraints declaration

type Category struct {
    Name        string `json:"name" gorm:"size:60;unique;not null"`
    Description string `json:"description" gorm:"size:120"`
    Parent      uint   `json:"parent"`
    Active      bool   `json:"active" gorm:"default:true"`
    gorm.Model
}

huangapple
  • 本文由 发表于 2022年4月19日 01:01:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/71914774.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定