Create and Drop Table

Bun provides a helper method to quickly drop and create tables:

err := db.ResetModel(ctx, (*Model1)(nil), (*Model2)(nil))
DROP TABLE IF EXISTS model1 CASCADE;
CREATE TABLE model1 (...);

DROP TABLE IF EXISTS model2 CASCADE;
CREATE TABLE model2 (...);

Create Table

For the full list of supported methods, see API referenceopen in new window.

db.NewCreateTable().

	Model(&strct).

	Table("table1"). // quotes table names
	TableExpr("table1"). // arbitrary unsafe expression
	ModelTableExpr("table1"). // overrides model table name

	Temp().
	IfNotExists().
	Varchar(100). // turns VARCHAR into VARCHAR(100)

	ForeignKey(`(fkey) REFERENCES table1 (pkey) ON DELETE CASCADE`).
	PartitionBy("HASH (id)").
	TableSpace("fasttablespace").

	Exec(ctx)

To create a table:

_, err := db.NewCreateTable().
	Model((*Book)(nil)).
	ForeignKey(`("author_id") REFERENCES "users" ("id") ON DELETE CASCADE`).
	Exec(ctx)
if err != nil {
	panic(err)
}

You can also modify query from the bun.BeforeCreateTableHook hook.

var _ bun.BeforeCreateTableHook = (*Book)(nil)

func (*Book) BeforeCreateTable(ctx context.Context, query *bun.CreateTableQuery) error {
	query.ForeignKey(`("author_id") REFERENCES "users" ("id") ON DELETE CASCADE`)
	return nil
}

if _, err := db.NewCreateTable().Model((*Book)(nil)).Exec(ctx); err != nil {
	panic(err)
}

To create an index on the table, you can use bun.AfterCreateTableHook hook:

var _ bun.AfterCreateTableHook = (*Book)(nil)

func (*Book) AfterCreateTable(ctx context.Context, query *bun.CreateTableQuery) error {
	_, err := query.DB().NewCreateIndex().
		Model((*Book)(nil)).
		Index("category_id_idx").
		Column("category_id").
		Exec(ctx)
	return err
}

See exampleopen in new window for details.

Drop Table

For the full list of supported methods, see API referenceopen in new window.

db.NewDropTable().

	Model(&strct).

	Table("table1"). // quotes table names
	TableExpr("table1"). // arbitrary unsafe expression
	ModelTableExpr("table1"). // overrides model table name

	IfExists().

	Cascade().
	Restrict().

	Exec(ctx)

To drop a table:

_, err := db.NewDropTable().Model((*Book)(nil)).Exec(ctx)
if err != nil {
	panic(err)
}
Get insights and updates in your inbox: