0

I am using SQLAlchemy in Flask to connect to my Postgres server, and now I want to execute some raw SQL to insert a column into a table. I am getting this error, however:

sqlalchemy.exc.ProgrammingError: (ProgrammingError) syntax error at or near "user"
LINE 1: ALTER TABLE user ADD COLUMN permissions INTEGER
                    ^
 'ALTER TABLE user ADD COLUMN permissions INTEGER' {}

As you can see, it says there is an SQL error, although I have no idea what I could be doing wrong.

This is the very simple function that executes the command:

@staticmethod
def addColumn():
    db.engine.execute('ALTER TABLE user ADD COLUMN permissions INTEGER')

The db object otherwise works perfectly, and there is nothing wrong with the connection or anything of the sort.

I feel like I'm overlooking something very simple, but I just can't figure out what it is. Does anybody have any idea?

2
  • Does it work when you put your table name between double quotes? Also bear in mind that postgres names are case-sensitive. Commented Aug 5, 2013 at 20:09
  • That was it, Hyperboreus! I never knew double quotes were needed there. Thanks! Commented Aug 5, 2013 at 20:11

2 Answers 2

2

The PostgreSQL docs say that USER is a reserved keyword, and needs to be quoted to be used as an identifier.

Key Word      PostgreSQL      SQL 99      SQL 92
USER          reserved        reserved    reserved
Sign up to request clarification or add additional context in comments.

Comments

1

Is user a reserved word and thus needing to be referenced specially in SQL statement?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.