MCPcopy Index your code
hub / github.com/go-pg/pg

github.com/go-pg/pg @v10.15.1 sqlite

repository ↗ · DeepWiki ↗ · release v10.15.1 ↗
1,789 symbols 5,874 edges 157 files 235 documented · 13% 105 cross-repo links
README

PostgreSQL client and ORM for Golang

Maintenance mode

go-pg is in a maintenance mode and only critical issues are addressed. New development happens in Bun repo which offers similar functionality but works with PostgreSQL, MySQL, MariaDB, and SQLite.

Golang ORM


Go PkgGoDev Documentation Chat

Tutorials

Ecosystem

Features

Installation

go-pg supports 2 last Go versions and requires a Go version with modules support. So make sure to initialize a Go module:

go mod init github.com/my/repo

And then install go-pg (note v10 in the import; omitting it is a popular mistake):

go get github.com/go-pg/pg/v10

Quickstart

package pg_test

import (
    "fmt"

    "github.com/go-pg/pg/v10"
    "github.com/go-pg/pg/v10/orm"
)

type User struct {
    Id     int64
    Name   string
    Emails []string
}

func (u User) String() string {
    return fmt.Sprintf("User<%d %s %v>", u.Id, u.Name, u.Emails)
}

type Story struct {
    Id       int64
    Title    string
    AuthorId int64
    Author   *User `pg:"rel:has-one"`
}

func (s Story) String() string {
    return fmt.Sprintf("Story<%d %s %s>", s.Id, s.Title, s.Author)
}

func ExampleDB_Model() {
    db := pg.Connect(&pg.Options{
        User: "postgres",
    })
    defer db.Close()

    err := createSchema(db)
    if err != nil {
        panic(err)
    }

    user1 := &User{
        Name:   "admin",
        Emails: []string{"admin1@admin", "admin2@admin"},
    }
    _, err = db.Model(user1).Insert()
    if err != nil {
        panic(err)
    }

    _, err = db.Model(&User{
        Name:   "root",
        Emails: []string{"root1@root", "root2@root"},
    }).Insert()
    if err != nil {
        panic(err)
    }

    story1 := &Story{
        Title:    "Cool story",
        AuthorId: user1.Id,
    }
    _, err = db.Model(story1).Insert()
    if err != nil {
        panic(err)
    }

    // Select user by primary key.
    user := &User{Id: user1.Id}
    err = db.Model(user).WherePK().Select()
    if err != nil {
        panic(err)
    }

    // Select all users.
    var users []User
    err = db.Model(&users).Select()
    if err != nil {
        panic(err)
    }

    // Select story and associated author in one query.
    story := new(Story)
    err = db.Model(story).
        Relation("Author").
        Where("story.id = ?", story1.Id).
        Select()
    if err != nil {
        panic(err)
    }

    fmt.Println(user)
    fmt.Println(users)
    fmt.Println(story)
    // Output: User<1 admin [admin1@admin admin2@admin]>
    // [User<1 admin [admin1@admin admin2@admin]> User<2 root [root1@root root2@root]>]
    // Story<1 Cool story User<1 admin [admin1@admin admin2@admin]>>
}

// createSchema creates database schema for User and Story models.
func createSchema(db *pg.DB) error {
    models := []interface{}{
        (*User)(nil),
        (*Story)(nil),
    }

    for _, model := range models {
        err := db.Model(model).CreateTable(&orm.CreateTableOptions{
            Temp: true,
        })
        if err != nil {
            return err
        }
    }
    return nil
}

See also

Extension points exported contracts — how you extend this code

ColumnScanner (Interface)
ColumnScanner is used to scan column values. [9 implementers]
orm/orm.go
BeforeInsertHook (Interface)
------------------------------------------------------------------------------ [5 implementers]
orm/hook.go
ValueAppender (Interface)
(no doc) [10 implementers]
types/types.go
HooklessModel (Interface)
(no doc) [6 implementers]
orm/model.go
QueryHook (Interface)
QueryHook ... [3 implementers]
hook.go
QueryFormatter (Interface)
------------------------------------------------------------------------------ [3 implementers]
orm/format.go
Error (Interface)
Error represents an error returned by PostgreSQL server using PostgreSQL ErrorResponse protocol. https://www.postgresql [2 …
error.go
DBI (Interface)
DBI is a DB interface implemented by *DB and *Tx. [2 implementers]
pg.go

Core symbols most depended-on inside this repo

Model
called by 289
pg.go
NewQuery
called by 141
orm/query.go
Close
called by 102
internal/pool/pool.go
Relation
called by 101
orm/model_table.go
Kind
called by 89
orm/model_table.go
Select
called by 88
orm/join.go
Where
called by 78
orm/query.go
Exec
called by 77
pg.go

Shape

Method 954
Function 538
Struct 245
Interface 35
TypeAlias 15
FuncType 2

Languages

Go100%

Modules by API surface

orm/query.go140 symbols
example_model_test.go56 symbols
orm/table.go55 symbols
internal/pool/pool.go51 symbols
orm/hook.go45 symbols
messages.go45 symbols
pg.go41 symbols
base.go41 symbols
orm/model_table_struct.go38 symbols
db_test.go38 symbols
tx.go36 symbols
orm/format.go34 symbols

Dependencies from manifests, versioned

github.com/go-pg/zerocheckerv0.2.0 · 1×
github.com/jinzhu/inflectionv1.0.0 · 1×
github.com/klauspost/cpuid/v2v2.0.9 · 1×
github.com/kr/textv0.1.0 · 1×
github.com/niemeyer/prettyv0.0.0-2020022712484 · 1×
github.com/nxadm/tailv1.4.4 · 1×

Datastores touched

dbDatabase · 1 repos
postgresDatabase · 1 repos

For agents

$ claude mcp add pg \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact