gpt4 book ai didi

使用 golang-migrate/migrate 进行 Gorm 迁移

转载 作者:行者123 更新时间:2023-12-04 11:15:02 48 4
gpt4 key购买 nike

我决定使用 gorm作为我的 ORM。我想使用 golang-migrate/migrate 进行迁移因为,看起来 GORM 没有版本化迁移文件。我宁愿使用 CLI 进行迁移,而不是使用自动迁移。
我阅读了 gorm 文档,但我没有看到 gorm 如何将模型转换为 SQL 表。有没有关于为 gorm 生成的 SQL 表的任何示例或文档? (尤其是类型或关联如何映射到 SQL)

最佳答案

read the gorm documentation, but I didn't see how gorm translate the models into SQL Table. Is there any example or documentation about the generated SQL table for gorm?? (especially how types or association mapped to SQL)


Declaring Models段落:

column data type, prefer to use compatible general type, e.g: bool, int, uint, float, string, time, bytes, which works for all databases, and can be used with other tags together, like not null, size, autoIncrement… specified database data type like varbinary(8) also supported, when using specified database data type, it needs to be a full database data type, for example: MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT


例如
type Post struct {
ID uuid.UUID `gorm:"type:uuid;default:uuid_generate_v4()"`
Title string
Tags pq.StringArray `gorm:"type:text[]"`
}
👉🏻 还有看 Customize Data Types段落。

关于使用 golang-migrate/migrate 进行 Gorm 迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64510093/

48 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com