Skip to content
Advertisement

Creating Postgres View getting ERROR: column “id” specified more than once

SCENARIO:

I have this select statement that JOINs a bunch of tables together:

I’d like to create a Postgres View. So wrote it out like this:

I keep getting this error:

ERROR: column “id” specified more than once

QUESTIONS:

  1. How do I fix this error? I would like to create a view called “all_events”.
  2. Are Postgres View sort of like aliases in other languages?

New to Postgres, reading the docs but trying to understand the mental model here.

Advertisement

Answer

You have several column names that are the same. Even if you select e0.id the column is still name (only) id.

But in the scope of a view (or table) each column name must be unique.

You need to provide aliases for each duplicate column:


Although Postgres allows it, I highly recommend to not create a view with an ORDER BY statement. If you ever sort the results of that view by a different column, Postgres will sort the data twice.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement