Skip to Content
fusion-orm
Models

Base models

fusion.Model and fusion.UUIDModel — autoincrement and UUIDv7 primary keys, automatic timestamps, and soft delete.

fusion-orm ships two base models with gorm parity (model_base.go:3-4). Embed one by value and your model inherits the primary key, automatic CreatedAt/UpdatedAt, and a soft-delete DeletedAt:

// Model is the numeric base model, mirroring gorm.Model: an autoincrement
// primary key backfilled after insert, automatic CreatedAt/UpdatedAt, and a
// nullable DeletedAt that enables soft delete.
type Model struct {
	ID        uint64 `fusion:"pk,autoincrement"`
	CreatedAt time.Time
	UpdatedAt time.Time
	DeletedAt DeletedAt `fusion:"index"`
}
// UUIDModel replaces Model's numeric ID with a UUIDv7 primary key generated
// by the ORM at create time whenever the field is zero. Pre-set IDs are
// respected.
type UUIDModel struct {
	ID        uuid.UUID `fusion:"pk,type:uuid"`
	CreatedAt time.Time
	UpdatedAt time.Time
	DeletedAt DeletedAt `fusion:"index"`
}

What each base gives you

fusion.Modelfusion.UUIDModel
Key typeuint64, pk,autoincrementuuid.UUID, pk,type:uuid
Key sourcedatabase auto-increment, backfilled after insertUUIDv7 generated by the ORM when the field is zero
Pre-set IDs—respected: a non-zero ID is inserted as-is
TimestampsCreatedAt/UpdatedAt auto-setsame
Soft deleteDeletedAt fusion.DeletedAt with an indexsame

DeletedAt is a nullable timestamp wrapper (soft_delete.go:15-21): its zero value means the row is live, so the default query condition is deleted_at IS NULL. The index tag gives it a non-unique index — soft-delete filtering is on every query, so the index earns its keep.

The UUIDv7 key

The generator is attached at parse time: any primary key that is uuid.UUID (or *uuid.UUID) without autoincrement gets filled with uuid.NewV7() at create when the field is zero (schema/field.go:268-281). UUIDv7 is time-ordered, so it indexes well in B-trees and sorts chronologically.

post := postWithUUID{Title: "hello"}
if err := db.WithContext(ctx).Create(&post).Error; err != nil {
	t.Fatalf("create: %v", err)
}
if post.ID == (uuid.UUID{}) {
	t.Fatal("id not generated")
}
if post.ID[6]>>4 != 7 {
	t.Errorf("version nibble = %d, want 7", post.ID[6]>>4)
}

A pre-set ID is inserted verbatim — the generator only fires on the zero value (model_test.go:63-74).

Embedding

type userWithUUIDModel struct {
	UUIDModel
	Email string
}

func (userWithUUIDModel) TableName() string { return "base_uuid_users" }

type userWithNumericModel struct {
	Model
	Email string
}

func (userWithNumericModel) TableName() string { return "base_users" }

The embedded struct flattens exactly like an anonymous embed (defining models): id, created_at, updated_at, deleted_at, email — with all conventions intact.

What AutoMigrate produces

The type:uuid tag is a portable alias: native uuid on PostgreSQL, TEXT on SQLite (migrator/migrator.go:151-158). Derived from the column-definition builder (migrator/migrator.go:161-233) and index creator (migrator/migrator.go:236-256), AutoMigrate(&userWithUUIDModel{}) issues:

CREATE TABLE "base_uuid_users" ("id" uuid PRIMARY KEY, "created_at" TIMESTAMP, "updated_at" TIMESTAMP, "deleted_at" TIMESTAMP)
CREATE INDEX IF NOT EXISTS "idx_base_uuid_users_deleted_at" ON "base_uuid_users" ("deleted_at")

On PostgreSQL an autoincrement primary key instead renders BIGSERIAL (migrator/migrator.go:164-166); SQLite uses INTEGER PRIMARY KEY AUTOINCREMENT.

Verified against the migrator tests

The SQLite shape is pinned by model_test.go:112-141: AutoMigrate on userWithUUIDModel produces an id column of type TEXT plus the deleted_at index.

Soft delete lifecycle

Delete writes deleted_at; queries filter it out until you opt back in:

if err := db.WithContext(ctx).Delete(&user).Error; err != nil {
	t.Fatal(err)
}
var count int64
if err := db.WithContext(ctx).Model(&userWithUUIDModel{}).Count(&count).Error; err != nil || count != 0 {
	t.Errorf("soft-deleted row visible: %d, %v", count, err)
}
var hidden userWithUUIDModel
if err := db.WithContext(ctx).Unscoped().First(&hidden, "id = ?", user.ID).Error; err != nil {
	t.Fatalf("unscoped first: %v", err)
}

Unscoped() disables the filter for that chain — reads see the row, and a delete under Unscoped issues a real DELETE. Restore clears deleted_at again (soft_delete.go:227-262). Full details in soft delete.

Next

On this page