uptrace/bun

Why not use struct tags for index creation in Bun?

Open

#1,161 建立於 2025年3月29日

在 GitHub 查看
 (5 留言) (0 反應) (1 負責人)Go (4,826 star) (279 fork)user submission
enhancementhelp wanted

描述

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.

貢獻者指南