Insert [PostgreSQL MySQL]

API

For the full list of supported methods, see InsertQueryINSERT - 图1open in new window.

  1. db.NewInsert().
  2. With("cte_name", subquery).
  3. Model(&strct).
  4. Model(&slice).
  5. Model(&map). // only map[string]interface{}
  6. Column("col1", "col2"). // list of columns to insert
  7. ExcludeColumn("col1"). // all columns except col1
  8. ExcludeColumn("*"). // exclude all columns
  9. Table("table1", "table2"). // quotes table names
  10. TableExpr("table1 AS t1"). // arbitrary unsafe expression
  11. TableExpr("(?) AS subq", subquery).
  12. ModelTableExpr("table1 AS t1"). // overrides model table name
  13. Value("col1", "expr1", arg1, arg2). // overrides column value
  14. On("CONFLICT (id) DO UPDATE").
  15. Set("col1 = EXCLUDED.col1").
  16. WherePK(). // where using primary keys
  17. Where("id = ?", 123).
  18. Where("name LIKE ?", "my%").
  19. Where("? = 123", bun.Ident("id")).
  20. Where("id IN (?)", bun.In([]int64{1, 2, 3})).
  21. Where("id IN (?)", subquery).
  22. Where("FALSE").WhereOr("TRUE").
  23. WhereGroup(" AND ", func(q *bun.SelectQuery) *bun.SelectQuery {
  24. return q.WhereOr("id = 1").
  25. WhereOr("id = 2")
  26. }).
  27. Returning("*").
  28. Returning("col1, col2").
  29. Returning("NULL"). // don't return anything
  30. Exec(ctx)

Example

To insert data, define a model and use InsertQueryINSERT - 图2open in new window:

  1. book := &Book{Title: "hello"}
  2. res, err := db.NewInsert().Model(book).Exec(ctx)

Bulk-insert

To bulk-insert models, use a slice:

  1. books := []Book{book1, book2}
  2. res, err := db.NewInsert().Model(&books).Exec(ctx)
  3. if err != nil {
  4. panic(err)
  5. }
  6. for _, book := range books {
  7. fmt.Println(book.ID) // book id is scanned automatically
  8. }

Upsert

To insert a new book or update the existing one:

  1. _, err := db.NewInsert().
  2. Model(&book).
  3. On("CONFLICT (id) DO UPDATE").
  4. Set("title = EXCLUDED.title").
  5. Exec(ctx)
  1. INSERT INTO "books" ("id", "title") VALUES (100, 'my title')
  2. ON CONFLICT (id) DO UPDATE SET title = EXCLUDED.title

For MySQL use:

  1. _, err := db.NewInsert().
  2. Model(&book).
  3. On("DUPLICATE KEY UPDATE").
  4. Exec(ctx)
  1. INSERT INTO `books` (`id`, `title`) VALUES (100, 'my title')
  2. ON DUPLICATE KEY UPDATE `title` = VALUES(`title`)

To ignore duplicates, use Ignore with all databases:

  1. _, err := db.NewInsert().
  2. Model(&book).
  3. Ignore().
  4. Exec(ctx)
  1. -- MySQL
  2. INSERT IGNORE INTO `books` (`id`, `title`) VALUES (100, 'my title');
  3. -- PostgreSQL
  4. INSERT INTO `books` (`id`, `title`) VALUES (100, 'my title')
  5. ON CONFLICT DO NOTHING;

Maps

To insert a map[string]interface{}:

  1. values := map[string]interface{}{
  2. "title": "title1",
  3. "text": "text1",
  4. }
  5. _, err := db.NewInsert().Model(&values).TableExpr("books").Exec()
  1. INSERT INTO "books" ("title", "text") VALUES ('title1', 'text2')

INSERT … SELECT

To copy rows between tables:

  1. _, err := db.NewInsert().
  2. Table("books_backup").
  3. Table("books").
  4. Exec(ctx)
  1. INSERT INTO "books_backup" SELECT * FROM "books"

You can also specify columns to copy:

  1. _, err := db.NewInsert().
  2. ColumnExpr("id, name").
  3. Table("dest").
  4. Table("src").
  5. Exec(ctx)
  1. INSERT INTO "dest" (id, name) SELECT id, name FROM "src"