Skip to content

How to insert multiple values? #7

@baransu

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
|}];

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions