Skip to content
This repository was archived by the owner on Aug 25, 2021. It is now read-only.

Postgres tables are quoted when created #69

Closed
matthewmueller opened this issue Jul 24, 2019 · 3 comments
Closed

Postgres tables are quoted when created #69

matthewmueller opened this issue Jul 24, 2019 · 3 comments
Assignees
Labels
bug/0-needs-info More information is needed for reproduction. kind/bug A reported bug.

Comments

@matthewmueller
Copy link
Contributor

Based on: https://prisma.slack.com/archives/CKQTGR6T0/p1563892709024100

Screen Shot 2019-07-24 at 10 20 57 AM

I can confirm that with:

CREATE TABLE "public"."Post"("id" text NOT NULL  ,"createdAt" timestamp(3) NOT NULL  ,"updatedAt" timestamp(3) NOT NULL DEFAULT '1970-01-01 00:00:00' ,"published" boolean NOT NULL DEFAULT false ,"title" text NOT NULL DEFAULT '' ,"content" text   ,PRIMARY KEY ("id"));

You can only select the table via select * from "Post".
This doesn't work: select * from Post.

We need to be smarter about what tables need quotes and what tables do not.

@matthewmueller matthewmueller added scope/m bug/2-confirmed We have confirmed that this is a bug. kind/bug A reported bug. labels Jul 24, 2019
@matthewmueller matthewmueller changed the title Tables are quoted when created Postgres tables are quoted when created Jul 24, 2019
@mikeslade
Copy link

@matthewmueller this behavior is found in the fields on the tables as well.

CREATE TABLE "public"."Agent"("id" text NOT NULL ,"firstName" text NOT NULL DEFAULT '' ,"middleName" text ,"lastName" text NOT NULL DEFAULT '' ,"suffix" text ,"recordStatus" text NOT NULL DEFAULT 'ACTIVE' ,"createdAt" timestamp(3) NOT NULL ,"updatedAt" timestamp(3) NOT NULL DEFAULT '1970-01-01 00:00:00' ,PRIMARY KEY ("id"));

None of these are reserved words in Postgres, but result in table creation like depicted below:

image

Which then requires queries like this to fetch items:

SELECT "firstName", "lastName", suffix FROM "Agent"

Looking further, it seems that Postgres just forces everything to lowercase if it is not surrounded by quotes. Given that the docs recommend PascalCase for models and camelCase for field names, I understand why the team simply wrapped everything in quotes. It is the only way to preserve the case-sensitive formatting in the table names and the field names. This does create some inconsistency for field names without capital letters, since Postgres drops the quotes on its own in that scenario.

@matthewmueller
Copy link
Contributor Author

Leaving this here as well for reference: https://lerner.co.il/2013/11/30/quoting-postgresql/

@mavilein mavilein removed their assignment Jul 31, 2019
@timsuchanek timsuchanek added kind/feature A request for a new feature. kind/question Developer asked a question. No code changes required. and removed bug/2-confirmed We have confirmed that this is a bug. kind/bug A reported bug. labels Aug 5, 2019
@matthewmueller
Copy link
Contributor Author

It turns out postgres is a bit weird and if we want capitalized models (to match the schema), we'll need to use double quotes. It looks like ActiveRecord also does it this way.

Would a possible solution be to lowercase everything? It would mean that BlogTag would become blogtag in the database but that may be ok.

@janpio janpio added bug/0-needs-info More information is needed for reproduction. kind/bug A reported bug. and removed kind/feature A request for a new feature. kind/question Developer asked a question. No code changes required. labels Apr 27, 2020
@Jolg42 Jolg42 closed this as completed May 26, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug/0-needs-info More information is needed for reproduction. kind/bug A reported bug.
Projects
None yet
Development

No branches or pull requests

7 participants