uptrace/bun

Why not use struct tags for index creation in Bun?

Open

#1,161 opened on Mar 29, 2025

View on GitHub
 (5 comments) (0 reactions) (1 assignee)Go (4,826 stars) (279 forks)user submission
enhancementhelp wanted

Description

I'm currently working with Bun ORM in my Go project and I'm curious about the design decision behind index creation. While Bun uses struct tags for many database-related features (primary keys, default values, etc.), indexes need to be created using the CreateIndex API.

// Model definition with tags for other features
type WalletLog struct {
	Id        int `bun:",pk,autoincrement"`
	WalletId  int // No way to define index via tag
	Balance   int64
	CreatedAt time.Time    `bun:",notnull,default:current_timestamp"`
	UpdatedAt bun.NullTime `bun:",nullzero"`
	DeletedAt bun.NullTime `bun:",soft_delete,nullzero"`
}

// Separate index creation
_, err := db.NewCreateIndex().
    Model((*WalletLog)(nil)).
    Index("idx_wallet_log_wallet_id").
    Column("wallet_id").
    IfNotExists().
    Exec(ctx)

I'm curious about the reasoning behind this design choice. Many other ORMs support index creation via struct tags, which keeps the schema definition centralized in the model structs.

Was there a specific technical limitation or design philosophy that led to separating index creation from struct tags? Are there advantages to this approach that I might be missing?

Thank you for your insights.

Contributor guide

Why not use struct tags for index creation in Bun? · uptrace/bun#1161 | Good First Issue