Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Introspection] dot (and other non-[a-z0-9] characters) in field name #1106

Closed
janpio opened this issue Dec 6, 2019 · 4 comments
Closed
Assignees
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. topic: introspection
Milestone

Comments

@janpio
Copy link
Member

janpio commented Dec 6, 2019

e.g. Vanilla schema

@sorenbs
Copy link
Member

sorenbs commented Dec 8, 2019

Column names / field names

Background

In SQL, an identifier can be quoted. This generally mean that it is wrapped in a start and closing character. For Postgres this character is ". We always use quoted identifiers as this has no downsides and allow us to support the widest range of identifier names.

MySQL

Extended: U+0080 .. U+FFFF

https://dev.mysql.com/doc/refman/8.0/en/identifiers.html

Postgres

Quoted identifiers can contain any character, except the character with code zero. (To include a double quote, write two double quotes.) This allows constructing table or column names that would otherwise not be possible, such as ones containing spaces or ampersands. The length limitation still applies.

https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS

SQLite

No constraints

https://stackoverflow.com/questions/23770480/sqlite-table-and-column-name-requirements

MongoDB

Field names cannot contain the null character.
Top-level field names cannot start with the dollar sign ($) character.

https://docs.mongodb.com/manual/reference/limits/#Restrictions-on-Field-Names

Decision

We will support the full space of names for each supported database.
To that end, we will always use the quoted representation of an identifier.
Further, we will introduce the concept of a quoted identifier in the Prisma schema. We will only use the quoted form when needed, as it is ugly. See note below.

Note on the serialised Prisma Schema and spaces

Column and table names may contain spaces. We need to decide how we want to handle this in the Prisma Schema.

For example, a Integer column named age String would result in a schema like this:

model User {
  age String Int
}

To overcome this, we will introduce quoted identifiers:

model User {
  "age String" Int
}

The use of double quote is in line with the official SQL standard, but the resemblance is of no importance, as we also support non-SQL databases.

@sorenbs
Copy link
Member

sorenbs commented Dec 9, 2019

Temporary Guardrail

For now we will simply replace any invalid character with _ and add a @map() attribute

So:

model User {
  age String Int
}

Become

model User {
  age_String Int @map("age String")
}

@janpio janpio changed the title [Introspection] dot in field name [Introspection] dot (and other non-[a-z0-9] characters) in field name Dec 9, 2019
@janpio janpio transferred this issue from prisma/prisma-engines Dec 10, 2019
@janpio janpio added this to the Preview 19 milestone Dec 10, 2019
@pantharshit00 pantharshit00 added bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. labels Dec 11, 2019
@janpio janpio modified the milestones: Preview 19, Preview 20 Dec 19, 2019
@do4gr
Copy link
Member

do4gr commented Jan 6, 2020

Let's define some of the terms a little bit more:

Invalid character -> everything that does not match the regex _a-zA-Z0-9
Invalid at start of identifier -> everything that does not match the regex a-zA-Z

Replacement strategy:

Everything invalid before the first character valid for start of the the identifier gets dropped.
Everything invalid after the first character valid for the start of the identifier gets replaced with _.

@janpio
Copy link
Member Author

janpio commented Jan 7, 2020

fixed via #1139

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. topic: introspection
Projects
None yet
Development

No branches or pull requests

4 participants