Skip to content

Insert statement with WITH clause used as SELECT field #2886

Open
@kampde

Description

@kampde

Version

1.22.0

What happened?

This is similar to #2350.

I've tried 2 variations of the same query using WITH (v AS ...) and referencing that v as one of the SELECTed fields. While both variations produce the expected results using psql, they fail with different errors when compiling them using sqlc.

  1. Using `INSERT INTO ... WITH ... SELECT ...
-- name: GenerateNewHistoricalData :exec
INSERT INTO historical_data(id, default_value, version)
  WITH v AS (SELECT nextval('historical_data_version_seq'))
SELECT id, default_value, (table v)
FROM data;

Error: query.sql:18:1: relation "v" does not exist

  1. Using WITH ... INSERT INTO ... SELECT ...:
-- name: GenerateNewHistoricalDataTakeTwo :exec
WITH v AS (SELECT nextval('historical_data_version_seq'))
INSERT INTO historical_data(id, default_value, version)
SELECT id, default_value, (table v)
FROM data;

Error: query.sql:24:1: edit start location is out of bounds

Relevant log output

No response

Database schema

CREATE TABLE data (
  id   TEXT NOT NULL,
  value TEXT NOT NULL,
  CONSTRAINT data_pkey PRIMARY KEY(id)
);

CREATE TABLE historical_data (
  id TEXT NOT NULL,
  value TEXT NOT NULL,
  version BIGINT NOT NULL,
  CONSTRAINT historical_data_pkey PRIMARY KEY(id, version)
);

CREATE SEQUENCE historical_data_version_seq;

SQL queries

No response

Configuration

No response

Playground URL

https://play.sqlc.dev/p/4c877cf26c2bb27c217303995af1afbef3bf9c84a25e682f01de5f0745c259cc

What operating system are you using?

macOS

What database engines are you using?

PostgreSQL

What type of code are you generating?

Go

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions