Skip to content

How to insert multiple values? #7

Description

@baransu

I have following table:

let create_table = [%sqlf
  {|
    CREATE TABLE users (
      id INTEGER NOT NULL PRIMARY KEY,
      email VARCHAR NOT NULL,
      name VARCHAR NOT NULL
    )
  |}
];

And I want to insert all columns at once, possible adding multiple rows.

This query adds two rows with all columns:

let insert = [%sqlf {|
  INSERT INTO users
  VALUES (1, 'John', 'email1'), (2, 'Mary', 'email2')
  RETURNING id
|}];

but replacing (1, 'John', 'email1') with $user or $@user fails during compilation:
Error: [%sqlf]: ERROR: 42601: syntax error at or near "$1"
Would be amazing to have support for something like this:

let insert = [%sqlf {|
  INSERT INTO users
  VALUES $user
  RETURNING id
|}];

let insert = (~user: (int32, string, string)) => ...

it would be just a shortcut for

let insert = [%sqlf {|
  INSERT INTO users
  VALUES ($id, $email, $name)
  RETURNING id
|}];

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions