cannot get simple PostgreSQL insert to work

前端 未结 3 1413
长情又很酷
长情又很酷 2020-12-24 00:20

I\'m trying to do a simple insert into a postgres table, but am getting an error that the value I\'m trying to insert is being interpreted as a column name

I         


        
相关标签:
3条回答
  • 2020-12-24 00:31
    INSERT INTO "imageTagBusinessMainCategory"
    ("businessMainCategory")
    VALUES
    ('auto dealer')
    

    EDIT: Added double-quotes around the column name

    0 讨论(0)
  • 2020-12-24 00:40

    Postgres, Oracle etc expect the column name to be in quotes if they have mixed case. So either create a convention of all small or all caps for your table columns or use quotes as David Faber suggested

    INSERT INTO "imageTagBusinessMainCategory"
    ("businessMainCategory")
    VALUES
    ('auto dealer')
    
    0 讨论(0)
  • 2020-12-24 00:43

    Use 'auto dealer' instead. PostgreSQL interprets " as being quotes for identifiers, ' as being quotes for strings.

    Also:

    • If this is a new project, just don't use mixed case tables; it is a source of frustration later. Instead of being able to use any case in your SQL statements, you must both quote the identifier name and get the case correct.

    • There is no need to specify id/DEFAULT, you're asking it to do what it would have done already. I haven't met a DBMS that requires you to include columnName/DEFAULT if you want it to put the default value in the column, so I don't think this extra KV pair is going to make what is happening clearer to anyone reading your code later.

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