# ant > ant is a Go ORM. Models are plain structs, columns are field pointers that the compiler checks, and there is no code generation. ant is newer than most models' training data, and its API is not GORM's, Bun's or ent's. Don't guess from those: calls such as `db.Where("age > ?", 18)`, `Preload`, `AutoMigrate`, `Model(&u).Updates` and `First(&u)` don't exist. Check a name with `go doc github.com/skudra-go/ant ` or the pages below. ant's repo has a skill for coding agents, `skills/ant/SKILL.md`, which installs with `npx skills add skudra-go/ant` or, in Claude Code, `/plugin marketplace add skudra-go/ant`. The rules that matter most: - Models are plain structs. A database is a struct that embeds `ant.Context` and holds an `ant.Table[T]` per model; `ant.Open[AppDB](ctx, postgres.Dialect, dsn)` fills it in. The application imports the driver. - Columns are field pointers into a probe, never strings: `var Users = ant.Fields[User]()`, then `ant.Gt(&Users.Age, 18)`. The value must have the field's type. A pointer from another model's probe is `ant.ErrWrongModel` at run time. - Queries chain from a table and run at the call that takes `ctx`: `All` (returns `[]*T`), `First`, `Single`, `Find`, `Count`, `Exists`, `Each`. Builders never return errors; the terminal call does. - Relations are fields (`Posts []Post`, `User *User`) found by name (`Post.UserID`). They load only through `Include` or `IncludeOne`; an unloaded relation is `nil`. There is no lazy loading. - `Update` and `Delete` hang off a query, with explicit assignments: `db.Users.Where(...).Update(ctx, ant.Set(&Users.Age, 0))`. Without a condition they return `ant.ErrNoCondition`. - Loaded entities are tracked: change the struct and call `SaveChanges(ctx)`, which writes only the changed columns. A tracking context serves one operation at a time, so take `ant.Session(db)` per request or goroutine. Removing an element from a loaded relation slice deletes that row on the next save. - `db.Tx(ctx, func(tx *AppDB) error { ... })` runs a transaction; use `tx` inside it, not `db`. - Match errors with `errors.Is`: `ant.ErrNotFound`, `ant.ErrConcurrency`, `ant.ErrConcurrentUse`, `ant.ErrUnsupported`. - PostgreSQL has everything. MySQL, MariaDB, SQLite and SQL Server have queries, writes and transactions, but no migrations. Migrations are plain SQL that `go run ./cmd/migrate diff ` writes from the structs, and there are no down migrations. ## Get started - [ant](https://skudra.net/index.md): ant is a typed ORM for Go. Models are plain structs, queries are checked by the compiler, and there is no code generation. - [Quick start](https://skudra.net/quick-start.md): A small program that saves a user with posts, queries them, and updates a row, on SQLite with nothing to install or on PostgreSQL in Docker. - [Concepts](https://skudra.net/concepts.md): The ideas ant is built on, and the mistakes the compiler cannot catch. - [Why ant](https://skudra.net/why-ant.md): The problems ant was built to fix, next to how GORM, Bun and ent handle them. ## Models - [Defining models](https://skudra.net/models/defining-models.md): Structs as models, ant.Base, naming conventions, and how to override them. - [Field probes](https://skudra.net/models/field-probes.md): How &Users.Age names a column, what the compiler checks, and what it leaves to run time. - [Keys](https://skudra.net/models/keys.md): Primary keys by convention, explicit, composite, generated by the database, and generated in Go. - [Nullable columns and defaults](https://skudra.net/models/nullable-and-defaults.md): Pointers and sql.Null for NULL, and ant:"default" for database defaults. - [Field roles](https://skudra.net/models/field-roles.md): Tags that decide how a field is read and written. - [Configure](https://skudra.net/models/configure.md): Override conventions in typed Go with the Configure method of a model. - [Embedded structs and mixins](https://skudra.net/models/embedded-and-mixins.md): Group columns in a struct, and share indexes, checks and filters between models. - [Shared tables and views](https://skudra.net/models/shared-tables-and-views.md): Single-table inheritance, table splitting, views, and schemas. - [Custom types](https://skudra.net/models/custom-types.md): Named types, Scanner and Valuer, ant.JSON, and PostgreSQL types as column types. ## Queries - [Conditions](https://skudra.net/queries/conditions.md): Filtering rows with Where, from simple comparisons to combined and multi-column conditions. - [Reading rows](https://skudra.net/queries/reading-rows.md): All, First, Single, Find, Each, Pluck, Count and the other calls that run a query. - [Ordering and paging](https://skudra.net/queries/ordering-and-paging.md): OrderBy, Limit and Offset, numbered pages, cursors and chunks. - [Aggregates and grouping](https://skudra.net/queries/aggregates-and-grouping.md): Sums, counts, GROUP BY and HAVING, with typed results. - [Expressions](https://skudra.net/queries/expressions.md): Compute values in SQL, with their Go type checked. - [Projections](https://skudra.net/queries/projections.md): Shape results into your own types, in Go or in SQL. - [Joins](https://skudra.net/queries/joins.md): Follow relations, or join on any condition, across up to four models. - [Subqueries, sets, and CTEs](https://skudra.net/queries/subqueries-and-sets.md): Queries inside queries, unions, WITH clauses, recursion, and rows from memory. - [Window functions](https://skudra.net/queries/window-functions.md): Compute over related rows without collapsing them. - [Composing queries](https://skudra.net/queries/composing.md): Reuse queries, add to them conditionally, and name them. - [Raw SQL](https://skudra.net/queries/raw-sql.md): Your own SQL, scanned into the same structs. - [Locks and hints](https://skudra.net/queries/locks-and-hints.md): Row locks, job queues, optimizer hints, and query comments. ## Relations - [Declaring relations](https://skudra.net/relations/declaring.md): Declare a relation as a field on the struct, found by name or configured in Go. - [Loading relations](https://skudra.net/relations/loading.md): Load related rows with Include, shape what loads, load later, and count related rows. - [Filtering by relations](https://skudra.net/relations/filtering.md): Has, HasNone, and HasOne match rows by the rows they relate to, without loading anything. - [Many-to-many](https://skudra.net/relations/many-to-many.md): Relations through a join table, and how to read and change the links. - [Advanced relations](https://skudra.net/relations/advanced.md): Through relations, polymorphic relations, scoped relations, latest-of-many, inverses, and touching a parent. ## Writing - [Insert](https://skudra.net/writing/insert.md): Insert one row, several, or a whole graph of related entities. - [Update and delete](https://skudra.net/writing/update-and-delete.md): Bulk update and delete with explicit assignments, the guard that stops accidents, and writes on derived sources. - [Upsert and merge](https://skudra.net/writing/upsert-and-merge.md): Insert or update on a conflict with Upsert, and MERGE for what an upsert cannot do. - [Bulk writes](https://skudra.net/writing/bulk.md): InsertMany, UpdateMany, InsertFrom, Truncate, and PostgreSQL's COPY and batches. ## Change tracking - [SaveChanges](https://skudra.net/tracking/save-changes.md): Change what you loaded, and SaveChanges writes only the differences, in one transaction. - [Sessions](https://skudra.net/tracking/sessions.md): One tracking context per unit of work, and what happens when two operations share one. - [Changing relations](https://skudra.net/tracking/changing-relations.md): Edit a loaded relation like a slice, and SaveChanges inserts, updates, deletes, unlinks and moves the rows. - [Entity states](https://skudra.net/tracking/entity-states.md): Mark entities as new, saved, changed or removed, and inspect what the tracker knows. ## Transactions - [Transactions](https://skudra.net/transactions.md): Closures, nesting, savepoints, isolation levels, and how SaveChanges and rollbacks work together. - [Retries](https://skudra.net/transactions/retries.md): Run a transaction again after a deadlock or serialization conflict. - [Connections](https://skudra.net/transactions/connections.md): Open, wrap a pool, share transactions, and use read replicas. ## Lifecycle - [Hooks and observers](https://skudra.net/lifecycle/hooks-and-observers.md): Run code around writes with model methods, and after the commit with observers. - [Validation](https://skudra.net/lifecycle/validation.md): Check entities before they are inserted or updated. - [Timestamps](https://skudra.net/lifecycle/timestamps.md): CreatedAt and UpdatedAt are set by ant on any model that has them. - [Soft deletes](https://skudra.net/lifecycle/soft-deletes.md): Mark rows as deleted instead of removing them, so a mistake can be undone. - [Global filters](https://skudra.net/lifecycle/global-filters.md): A condition added to every query of a model, built from the call's context. - [Optimistic concurrency](https://skudra.net/lifecycle/optimistic-concurrency.md): Detect conflicting writes with a version field instead of a lock. ## Migrations - [Migrations overview](https://skudra.net/migrations/overview.md): Your structs are the source of truth, and migrations are plain SQL files that diff writes and up applies. - [Models to tables](https://skudra.net/migrations/models-to-tables.md): How Go types, relations, and configuration become a schema. - [Generated SQL](https://skudra.net/migrations/generated-sql.md): What diff writes for each kind of change, which changes it warns about, and how to edit the result. - [Hand-written changes](https://skudra.net/migrations/hand-written.md): Views, functions, data migrations, seeds, and statements outside a transaction. - [Deploying](https://skudra.net/migrations/deploying.md): Embed migrations in a binary, and choose when they run. - [Working in a team](https://skudra.net/migrations/teams.md): What happens when two branches add a migration, and squashing history. - [Adopting a database](https://skudra.net/migrations/adopting.md): Start from a schema you already have, with drift as the check. - [Configuration](https://skudra.net/migrations/configuration.md): The migrate.Config fields, with their defaults, and how to replace storage, file names and the SQL text. - [Commands](https://skudra.net/migrations/commands.md): Every migrate command, in one place. - [Troubleshooting](https://skudra.net/migrations/troubleshooting.md): What each migration failure means, and what to do about it. ## Tooling - [Inspecting queries](https://skudra.net/tooling/inspecting-queries.md): See the SQL, the plan, and what a piece of code really writes. - [Logging](https://skudra.net/tooling/logging.md): Statements through log/slog, with values kept out by default. - [Interceptors and events](https://skudra.net/tooling/interceptors.md): Hook into every statement, every save, and every change of state. - [Diagnostics](https://skudra.net/tooling/diagnostics.md): Warnings about N+1 patterns, slow requests, and row explosions. - [Errors](https://skudra.net/tooling/errors.md): Every error ant returns, how to match them, and how to read the failing SQL. - [Metrics and tracing](https://skudra.net/tooling/metrics-and-tracing.md): Prometheus metrics with no dependencies, and OpenTelemetry tracing in a module of its own. - [antvet](https://skudra.net/tooling/antvet.md): A go vet analyzer that finds ant mistakes the compiler cannot see. - [AI assistants](https://skudra.net/tooling/ai-assistants.md): Give a coding assistant the ant docs and skill, so it writes ant instead of guessing from GORM. ## Ship - [Testing](https://skudra.net/testing.md): Isolated tests against a real database, with factories and fixtures. ## Recipes - [Multi-tenancy](https://skudra.net/recipes/multi-tenancy.md): Keep customers apart in one database with a tenant filter that cannot be forgotten. - [Auditing](https://skudra.net/recipes/auditing.md): Four ways to record what happened, and which one fits. - [Encrypted fields](https://skudra.net/recipes/encrypted-fields.md): Store values with AES-256-GCM so a database dump reveals nothing. - [Partitioned tables](https://skudra.net/recipes/partitioned-tables.md): Monthly PostgreSQL partitions for append-mostly data with a retention rule. - [Several databases](https://skudra.net/recipes/several-databases.md): Use one context per database, and route requests to a database per tenant. - [When a commit fails](https://skudra.net/recipes/commit-failures.md): The one failure where you can't know what happened, and how to make the work safe to repeat. ## Reference - [Databases](https://skudra.net/reference/databases.md): What each of the five supported databases can do, and where they differ. - [Benchmarks](https://skudra.net/reference/benchmarks.md): ant against GORM, Bun, ent and plain database/sql. - [Compatibility](https://skudra.net/reference/compatibility.md): Supported Go and database versions, what counts as the API, and how to upgrade. - [Security](https://skudra.net/reference/security.md): How ant handles SQL, logs and secrets, and its known limits. ## Optional - [Full documentation](https://skudra.net/llms-full.txt): every page above in one file - [API reference](https://pkg.go.dev/github.com/skudra-go/ant@v0.3.1): every exported name, on pkg.go.dev - [Field pointers as column names: compile-checked Go queries without codegen](https://skudra.net/blog/field-pointers-as-column-names): How ant uses pointers to struct fields as column references, why the chain needed Go 1.27's generic methods, what it costs, and where it still fails.