Вопрос

I am wary of using quoted identifiers in PostgreSQL when my "business" table or column names happen to be reserved words. Are there any standard or best-practice mangling approaches? I would like to have a consistent mangling mechanism as there's also some code-generation in place and need to be able to automatically translate back and forth between mangled names used in PostgreSQL and non-mangled names used in code.

Это было полезно?

Решение

The simplest, and probably best, way to "mangle" terms to guarantee all names are not reserved words is to prefix them with an underscore character.

The best way is to not use reserved words - let the DBA chose the names for any clashes between business terms and DB terms.

Also consider terms that might clash with reserved words in your application language.

Другие советы

I like to reserve the underscore prefix (_name) for parameters and variables in server-side functions to rule out conflicts. I have seen that a lot lately among people working with functions.

For identifiers of database objects I pick the names by hand. Working with non-english terms (reduced to ASCII letters) avoids most of the conflicts.

Another alternative is to use the plural form for table names, since most reserved words are in singular form (a few exceptions!).

Most 1-letter prefixes would do the job without exception. Like: n_name. But since I hand-pick identifiers, there is no need for automation.

Anything is good that avoids the error-prone need for double-quotes. I never use reserved words even if Postgres would allow them.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top