Skip to content

Schema generation

Derevtsov Konstantin edited this page Jul 21, 2020 · 5 revisions

You can generate database schema for registered entities. Use Orm.GenerateSchema method:

// create connection and orm
cfg, err := pgx.ParseConfig(os.Getenv("DB"))
if err != nil {
    log.Fatal(err)
}

driver, err := d3pgx.NewPgxDriver(cfg)
if err != nil {
    log.Fatal(err)
}

d3orm := orm.NewOrm(driver)
// register entities
err = d3orm.Register(&entity1{}, &entity2{})
if err != nil {
    log.Fatal(err)
}
// generate schema sql - DDL commands
sql, err := d3orm.GenerateSchema()
if err != nil {
    log.Fatal(err)
}

// exec schema sql for creating tables and indexes
_, _ = driver.UnwrapConn().(*pgx.Conn).Exec(context.Background(), sql)
Clone this wiki locally