注意: AutoMigrate 会创建表、缺失的外键、约束、列和索引。 It will change existing column’s type if its size, precision changed, or if it’s changing from non-nullable to nullable. 出于保护您数据的目的,它 不会 删除未使用的列
If Replace is true, exec CREATE OR REPLACE otherwise exec CREATE.
If CheckOption is not empty, append to sql, e.g. WITH LOCAL CHECK OPTION.
NOTE SQLite currently does not support Replace in ViewOption
query := db.Model(&User{}).Where("age > ?", 20)
// Create View db.Migrator().CreateView("users_pets", gorm.ViewOption{Query: query}) // CREATE VIEW `users_view` AS SELECT * FROM `users` WHERE age > 20
// Create or Replace View db.Migrator().CreateView("users_pets", gorm.ViewOption{Query: query, Replace: true}) // CREATE OR REPLACE VIEW `users_pets` AS SELECT * FROM `users` WHERE age > 20
// Create View With Check Option db.Migrator().CreateView("users_pets", gorm.ViewOption{Query: query, CheckOption: "WITH CHECK OPTION"}) // CREATE VIEW `users_pets` AS SELECT * FROM `users` WHERE age > 20 WITH CHECK OPTION
// Drop View db.Migrator().DropView("users_pets") // DROP VIEW IF EXISTS "users_pets"
Constraints
type UserIndex struct { Name string`gorm:"check:name_checker,name <> 'jinzhu'"` }
type User struct { gorm.Model CreditCards []CreditCard }
type CreditCard struct { gorm.Model Number string UserID uint }
// create database foreign key for user & credit_cards db.Migrator().CreateConstraint(&User{}, "CreditCards") db.Migrator().CreateConstraint(&User{}, "fk_users_credit_cards") // ALTER TABLE `credit_cards` ADD CONSTRAINT `fk_users_credit_cards` FOREIGN KEY (`user_id`) REFERENCES `users`(`id`)
// check database foreign key for user & credit_cards exists or not db.Migrator().HasConstraint(&User{}, "CreditCards") db.Migrator().HasConstraint(&User{}, "fk_users_credit_cards")
// drop database foreign key for user & credit_cards db.Migrator().DropConstraint(&User{}, "CreditCards") db.Migrator().DropConstraint(&User{}, "fk_users_credit_cards")
Indexes
type User struct { gorm.Model Name string`gorm:"size:255;index:idx_name,unique"` }
// Create index for Name field db.Migrator().CreateIndex(&User{}, "Name") db.Migrator().CreateIndex(&User{}, "idx_name")
// Drop index for Name field db.Migrator().DropIndex(&User{}, "Name") db.Migrator().DropIndex(&User{}, "idx_name")
// Check Index exists db.Migrator().HasIndex(&User{}, "Name") db.Migrator().HasIndex(&User{}, "idx_name")
type User struct { gorm.Model Name string`gorm:"size:255;index:idx_name,unique"` Name2 string`gorm:"size:255;index:idx_name_2,unique"` } // Rename index name db.Migrator().RenameIndex(&User{}, "Name", "Name2") db.Migrator().RenameIndex(&User{}, "idx_name", "idx_name_2")
约束
GORM creates constraints when auto migrating or creating table, see Constraints or Database Indexes for details
Atlas Integration
Atlas is an open-source database migration tool that has an official integration with GORM.
While GORM’s AutoMigrate feature works in most cases, at some point you may need to switch to a versioned migrations strategy.
Once this happens, the responsibility for planning migration scripts and making sure they are in line with what GORM expects at runtime is moved to developers.
Atlas can automatically plan database schema migrations for developers using the official GORM Provider. After configuring the provider you can automatically plan migrations by running: