Skip to main content

Timestamps

ant fills fields named CreatedAt and UpdatedAt on every write, whether they come from ant.Base or are declared on the model.

Add the fields​

type Note struct {
ID int64
CreatedAt time.Time
UpdatedAt time.Time
Text string
}

What moves them​

ActionCreatedAtUpdatedAt
Insert, InsertMany, UpsertSetSet
Merge, row insertedSetSet
Merge, row matchedMoved
SaveChanges with a changeMoved
SaveChanges with no changeLeft alone
Unlinking a child from a to-many relationMoved
Bulk UpdateMoved
Soft delete and RestoreMoved
user.Name = "Carol"
err := db.SaveChanges(ctx)
// UPDATE "users" SET "name" = $1, "updated_at" = $2 WHERE "users"."id" = $3
_, err := db.Users.Where(ant.Eq(&Users.Name, "Alice")).Update(ctx, ant.Set(&Users.Age, 31))
// UPDATE "users" SET "age" = $1, "updated_at" = $2 WHERE "users"."name" = $3

Times are stored at the database's precision, so the value on your struct matches what is in the table.

Unlinking a child sets its foreign key to NULL with an UPDATE. It moves UpdatedAt and, on a versioned model, the version. Merge fills the same columns as Insert and Update, unless one of its assignments gives the column a value.

Set a time yourself​

A CreatedAt you set is kept on insert. An UpdatedAt the write sets wins over ant's, so imports and backfills keep their times:

_, err := db.Users.Where(ant.Eq(&Users.Name, "Alice")).Update(ctx, ant.Set(&Users.UpdatedAt, imported))
// UPDATE "users" SET "updated_at" = $1 WHERE "users"."name" = $2

Fix the clock in tests​

ant.WithNow replaces the clock ant reads, so tests can assert exact times.

now := time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC)

clock := ant.WithNow(func() time.Time { return now })

db, err := ant.Open[AppDB](ctx, postgres.Dialect, dsn, clock)

Store Unix seconds​

ant.UnixTime stores a time as an integer and is maintained the same way.

type Stamped struct {
ID int64
CreatedAt ant.UnixTime
UpdatedAt ant.UnixTime
}

Turn it off​

b.NoTimestamps stops ant from touching the fields on one model. Use it when another system owns them.

func (Note) Configure(b *ant.ModelBuilder[Note]) {
b.NoTimestamps()
}

To move a parent's UpdatedAt when a child is written, see Touch.