cockroachdb / cockroachdb/cockroach

Create view (using row(...)) raises [42703] ERROR even though the select query works

Open
#101,589 3 comments 0 reactions 0 assignees View on GitHub
C-bug O-community T-sql-queries
Dominant language
Go
Stars
32.5k
Forks
4.1k
PR merge metrics
PR metrics pending

Description

**Describe the problem**

I have a valid SELECT query. It doesn't raise any error when I run it, and gives me the expected result.
However when I try to create a view based on this same query, I get this error :
`[42703] ERROR: column "Id" does not exist`

**To Reproduce**

1. Create following tables

```sql
create table public."Users"
(
"Id" uuid default gen_random_uuid() not null constraint "PK_Users" primary key,
"UserName" varchar(32) default ''::STRING not null
);

create table public."Polls"
(
"Id" uuid default gen_random_uuid() not null constraint "PK_Polls" primary key,
"AuthorId" uuid not null,
"Title" varchar(125) not null,
"Tags" text not null,
"CreatedAt" timestamp with time zone not null
);

create table public."Polls.Options"
(
"Id" uuid default gen_random_uuid() not null constraint "PK_Polls.Options" primary key,
"Title" varchar(55) not null,
"PollId" uuid constraint "FK_Polls.Options_Polls_PollId" references public."Polls"
);

create table public."Polls.Options.Votes"
(
"Id" uuid default gen_random_uuid() not null constraint "PK_Polls.Options.Votes" primary key,
"VoterId" uuid not null,
"VotedAt" timestamp with time zone not null,
"PollOptionId" uuid constraint "FK_Polls.Options.Votes_Polls.Options_PollOptionId" references public."Polls.Options"
);

SET experimental_enable_temp_tables = 'on';

create temporary table poll_option_id_and_title_and_total_vote_count (
"Id" uuid,
"Title" varchar(55),
"VotesCount" bigint
);
```

2. Try to create the following view :

```sql
create view "Polls.GetPollFeed" as
select
"Polls"."Id" as "PollId",
"Polls"."Title",
string_to_array("Polls"."Tags", ',') as "Tags",
"Polls"."CreatedAt",
"Options",
"TotalVotesCount",
"Users"."UserName" AS "Author"
from "Polls"
join (
select
"PollId",
json_agg(
to_json(
row("Id", "Title", coalesce("VotesCount", 0))::poll_option_id_and_title_and_total_vote_count
)
) as "Options",
coalesce(sum("VotesCount"), 0) as "TotalVotesCount"
from "Polls.Options"
left join (
select "PollOptionId", count(*) as "VotesCount" from "Polls.Options.Votes" group by "PollOptionId"
) POV on "Id" = POV."PollOptionId"
group by "PollId"
) PO on "Polls"."Id" = PO."PollId"
join "Users" on "AuthorId" = "Users"."Id";
```
It raises `[42703] ERROR: column "Id" does not exist`

However if I run the select query without the `create view ... ` line, I get a valid result.

Note also that the error disappears when I replace `row("Id", "Title", coalesce("VotesCount", 0))::poll_option_id_and_title_and_total_vote_count` with simply `row()`.

Edit : note also that keeping `row("Id", "Title", coalesce("VotesCount", 0))` with just removing the cast to `poll_option_id_and_title_and_total_vote_count` makes it work.

**Expected behavior**
Expecting the view to be created without error

**Environment:**
- CockroachDB version : v22.2.7
- Cluster Serverless - AWS - eu-central-1
- Rider client with CoakroachDb driver (also tried with postgreSQL driver)

Jira issue: CRDB-27042

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.