name: Command Line

sort: 9

Command Line

You can call orm.RunCommand() after you registered models and database(s) as follows:

  1. func main() {
  2. // orm.RegisterModel...
  3. // orm.RegisterDataBase...
  4. ...
  5. orm.RunCommand()
  6. }
  1. go build main.go
  2. ./main orm
  3. # Get help by just run it.
  4. # If possible, go run main.go orm has the same result.

Database Schema Generation

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

Use the -force=1 flag to force drop tables and re-create.

Use the -v flag to print SQL statements.


Use program to create tables:

  1. // Database alias.
  2. name := "default"
  3. // Drop table and re-create.
  4. force := true
  5. // Print log.
  6. verbose := true
  7. // Error.
  8. err := orm.RunSyncdb(name, force, verbose)
  9. if err != nil {
  10. fmt.Println(err)
  11. }

Even if you do not enable force mode, ORM also will auto-add new fields and indexes, but you have to deal with delete operations yourself.

Print SQL Statements

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

Use database with alias default as default.