ORM tool

After registering model and database, call the RunCommand method to execute the ORM command。

  1. func main() {
  2. // ...registering models
  3. // ...registering database
  4. // don't forget import the driver
  5. orm.RunCommand()
  6. }
  1. go build main.go
  2. ./main orm

Creating table automatically

  1. ./main orm syncdb -h
  2. Usage of orm command: syncdb:
  3. -db="default": DataBase alias name
  4. -force=false: drop tables before create
  5. -v=false: verbose info

Beego will execute drop table before creating tables if you specify the flag -force=1.

And then use -v to check the SQL.

  1. name := "default"
  2. // drop tables
  3. force := true
  4. // print SQL
  5. verbose := true
  6. err := orm.RunSyncdb(name, force, verbose)
  7. if err != nil {
  8. fmt.Println(err)
  9. }

If you do not use the -force=1 flag, Beego will create new columns or create new indexes.

But if you wish to update the existing columns or indexes, you need to update them manually.

We have received some feedback mentioning that we would like to support deleting fields, or modifying the definition of a field. For now, we are not considering supporting this type of functionality. This is mainly from a risk perspective. Compared to adding fields, deleting such operations is much more dangerous and difficult to recover. So we are not very willing to expose this kind of functionality.

Print SQL

  1. ./main orm sqlall -h
  2. Usage of orm command: syncdb:
  3. -db="default": DataBase alias name