
This package is currently in maintenance mode, which means:
If looking for an actively maintained alternative, consider the following:
Bob is very similar to SQLBoiler. It was directly inspired by SQLBoiler and was created by a maintainer of SQLBoiler.
A comparison can be found here: https://bob.stephenafamo.com/vs/sqlboiler/.
sqlc is a command line tool that generates type-safe code from SQL.
It is not an ORM but for many use cases it can be a good alternative to SQLBoiler.
SQLBoiler is a tool to generate a Go ORM tailored to your database schema.
It is a "database-first" ORM as opposed to "code-first" (like gorm/gorp). That means you must first create your database schema. Please use something like sql-migrate or some other migration tool to manage this part of the database's life-cycle.
v1, v2, and v3 are no longer maintained.
v3 is the last GOPATH-compatible version.
v4 has no real breaking changes between v3 and itself other than Go modules and is the only maintained version. Note this does not work with GOPATH projects.
While attempting to migrate a legacy Rails database, we realized how much ActiveRecord benefited us in terms of development velocity.
Coming over to the Go database/sql package after using ActiveRecord feels extremely repetitive, super long-winded and down-right boring.
Being Go veterans we knew the state of ORMs was shaky, and after a quick review we found what our fears confirmed. Most packages out
there are code-first, reflect-based and have a very weak story around relationships between models. So with that we set out with these goals:
sql.DB code.We believe with SQLBoiler and our database-first code-generation approach we've been able to successfully meet all of these goals. On top of that SQLBoiler also confers the following benefits:
| Database | Driver Location |
|---|---|
| PostgreSQL | https://github.com/aarondl/sqlboiler/v4/drivers/sqlboiler-psql |
| MySQL | https://github.com/aarondl/sqlboiler/v4/drivers/sqlboiler-mysql |
| MSSQLServer 2012+ | https://github.com/aarondl/sqlboiler/v4/drivers/sqlboiler-mssql |
| SQLite3 | https://github.com/aarondl/sqlboiler/v4/drivers/sqlboiler-sqlite3 |
| CockroachDB | https://github.com/glerchundi/sqlboiler-crdb |
Note: SQLBoiler supports out of band driver support so you can make your own
We are seeking contributors for other database engines.
For a comprehensive list of available operations and examples please see Features & Examples.
import (
// Import this so we don't have to use qm.Limit etc.
. "github.com/aarondl/sqlboiler/v4/queries/qm"
)
// Open handle to database like normal
db, err := sql.Open("postgres", "dbname=fun user=abc")
if err != nil {
return err
}
// If you don't want to pass in db to all generated methods
// you can use boil.SetDB to set it globally, and then use
// the G variant methods like so (--add-global-variants to enable)
boil.SetDB(db)
users, err := models.Users().AllG(ctx)
// Query all users
users, err := models.Users().All(ctx, db)
// Panic-able if you like to code that way (--add-panic-variants to enable)
users := models.Users().AllP(db)
// More complex query
users, err := models.Users(Where("age > ?", 30), Limit(5), Offset(6)).All(ctx, db)
// Ultra complex query
users, err := models.Users(
Select("id", "name"),
InnerJoin("credit_cards c on c.user_id = users.id"),
Where("age > ?", 30),
AndIn("c.kind in ?", "visa", "mastercard"),
Or("email like ?", `%aol.com%`),
GroupBy("id", "name"),
Having("count(c.id) > ?", 2),
Limit(5),
Offset(6),
).All(ctx, db)
// Use any "boil.Executor" implementation (*sql.DB, *sql.Tx, data-dog mock db)
// for any query.
tx, err := db.BeginTx(ctx, nil)
if err != nil {
return err
}
users, err := models.Users().All(ctx, tx)
// Relationships
user, err := models.Users().One(ctx, db)
if err != nil {
return err
}
movies, err := user.FavoriteMovies().All(ctx, db)
// Eager loading
users, err := models.Users(Load("FavoriteMovies")).All(ctx, db)
if err != nil {
return err
}
fmt.Println(len(users.R.FavoriteMovies))
user_videos you should have: primary key(user_id, video_id), with both
user_id and video_id being foreign key columns to the users and videos
tables respectively and there are no other columns on this table.github.com/go-sql-driver/mysql driver, please activate
time.Time parsing when making your
MySQL database connection. SQLBoiler uses time.Time and null.Time to represent time in
it's models and without this enabled any models with DATE/DATETIME columns will not work._id.x_id will generate clearer method names.
It is advisable to use this naming convention whenever it makes sense for your database schema.--no-hooks flag. This will save you some binary size.If you like learning via a video medium, sqlboiler has a number of screencasts available.
NOTE: These videos predate modules (v4), the installation/import paths will be different though everything else should remain similar.
SQLBoiler: Advanced Queries and Relationships
Old (v2): SQLBoiler Screencast #1: How to get started
First you have to install the code generator binaries. There's the main binary and then a separate driver binary (select the right one for your database).
Be very careful when installing, there's confusion in the Go ecosystem and knowing what are the right commands to run for which Go version can be tricky. Ensure you don't forget any /v suffixes or you'll end up on an old version.
# Go 1.16 and above:
go install github.com/aarondl/sqlboiler/v4@latest
go install github.com/aarondl/sqlboiler/v4/drivers/sqlboiler-psql@latest
# Go 1.15 and below:
# Install sqlboiler v4 and the postgresql driver (mysql, mssql, sqlite3 also available)
# NOTE: DO NOT run this inside another Go module (like your project) as it will
# pollute your go.mod with a bunch of stuff you don't want and your binary
# will not get installed.
GO111MODULE=on go get -u -t github.com/aarondl/sqlboiler/v4
GO111MODULE=on go get github.com/aarondl/sqlboiler/v4/drivers/sqlboiler-psql
To install sqlboiler as a dependency in your project use the commands below
inside of your go module's directory tree. This will ins
$ claude mcp add sqlboiler \
-- python -m otcore.mcp_server <graph>