go-ozzo / ozzo-dbx

A Go (golang) package that enhances the standard database/sql package by providing powerful data retrieval methods as well as DB-agnostic query building capabilities.
MIT License
636 stars 91 forks source link

CRUD when PRIMARY KEY is a multiple-column index? #31

Closed kolkov closed 7 years ago

kolkov commented 7 years ago

Hi! How to use CRUD when PRIMARY KEY is a multiple-column index, not a single column?

qiangxue commented 7 years ago

Please read this section https://github.com/go-ozzo/ozzo-dbx#crud-operations (search for "composite")

kolkov commented 7 years ago

If I have this table structure with composite PK (movie_id, person_id, occupation_id)?

type DbMoviePersonMap struct {
    MovieId int `db:"pk"`
    PersonId int `db:"pk"`
    OccupationId int `db:"pk"`
    Importance int
    Note string
}

How can I get one row from this table? for example: movie_id: 1, person_id: 1, occupation_id: 1

Only this way?

// SELECT * FROM order_item WHERE order_id=100 AND item_id=20
db.Select().Where(dbx.HashExp{"order_id": 100, "item_id": 20}).One(&orderItem)
qiangxue commented 7 years ago

Yes, see doc:

// Model selects the row with the specified primary key and populates the model with the row data.
//
// The model variable should be a pointer to a struct. If the query does not specify a "from" clause,
// it will use the model struct to determine which table to select data from. It will also use the model
// to infer the name of the primary key column. Only simple primary key is supported. For composite primary keys,
// please use Where() to specify the filtering condition.
kolkov commented 7 years ago

Thanks!