Underscores or camelCase in PostgreSQL identifiers, when the programming language uses camelCase?

后端 未结 3 1691
悲&欢浪女
悲&欢浪女 2021-02-01 12:02

This has been bothering me for a while, and I can\'t arrive at a solution that feels right...

Given an OO language in which the usual naming convention for object

相关标签:
3条回答
  • 2021-02-01 12:38

    If your columns in the PostgreSQL are with underscores, you can put aliases but with doule-quotes.

    Example :

    SELECT my_column as "myColumn" from table;
    
    0 讨论(0)
  • 2021-02-01 12:45

    Given that PostgreSQL uses case-insensitive identifiers with underscores, should you change all your identifiers in your application to do the same? Clearly not. So why do you think the reverse is a reasonable choice?

    The convention in PostgreSQL has come about through a mix of standards compliance and long-term experience of its users. Stick with it.

    If translating between column-names and identifiers gets tedious, have the computer do it - they're good at things like that. I'm guessing almost all of the 9-million database abstraction libraries out there can do that. If you have a dynamic language it'll take you all of two lines of code to swap column-names to identifiers in CamelCase.

    0 讨论(0)
  • 2021-02-01 13:00

    I know this is late however for something that would be simple to translate on the fly, you could write a small help function that would live in your code as such:

    function FormatObjForDb(srcObj){

    const newObj = {};
    
    Object.keys(srcObj).forEach(key => newObj[key.toLowerCase()] = srcObj[key]);
    
    return newObj;
    

    }

    export const formatObjForDb = FormatObjForDb;

    0 讨论(0)
提交回复
热议问题