Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax error at or near "user" when adding Postgres constraint

Tags:

postgresql

I'm running Postgres 8.4.13, and trying to add a constraint to an existing table. According to the docs, this should be possible:

alter table indexed_friends add constraint no_duplicate_user_friends unique (user, friend); 

Yet when I run this I get the following error:

ERROR:  syntax error at or near "user" 

I'm confused because I'm following an unique constraint example listed in the documentation almost exactly. I can provide the table schema, but since it's complaining about a syntax error, I'm not sure that's necessary.

like image 704
Kevin Burke Avatar asked Jun 24 '13 01:06

Kevin Burke


People also ask

How to create constraint in PostgreSQL?

The syntax for creating a unique constraint using an ALTER TABLE statement in PostgreSQL is: ALTER TABLE table_name ADD CONSTRAINT constraint_name UNIQUE (column1, column2, ... column_n); table_name.

What is syntax error in PostgreSQL?

The error message “syntax error at or near 'grant'” is one of the most common PostgreSQL database errors. However, it can easily be identified and resolved. To understand this issue, you need to know that SQL distinguishes between reserved and non-reserved key word tokens.

How to use constraints in PostgreSQL?

A check constraint is the most generic constraint type. It allows you to specify that the value in a certain column must satisfy a Boolean (truth-value) expression. For instance, to require positive product prices, you could use: CREATE TABLE products ( product_no integer, name text, price numeric CHECK (price > 0) );

What is primary key in PostgreSQL?

What is a primary key in PostgreSQL? In PostgreSQL, a primary key is a single field or combination of fields that uniquely defines a record. None of the fields that are part of the primary key can contain a NULL value. A table can have only one primary key.


1 Answers

Ahhh... The word user is a reserved word in Postgres.

Surrounding it in quotes:

alter table indexed_friends add constraint no_duplicate_user_friends unique ("user", friend); 

worked.

like image 113
Kevin Burke Avatar answered Oct 05 '22 08:10

Kevin Burke