Syntax error at or near "user" when adding Postgres constraint - 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.

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.

Related

syntax error in my postgres statement for alter sequence

UPDATE:
using postgres 14,
I just get error :Query 1 ERROR: ERROR: syntax error at or near "ok"
This is my database:
-- Sequence and defined type
CREATE SEQUENCE IF NOT EXISTS id_seq;
-- Table Definition
CREATE TABLE "public"."ok" (
"id" int8 NOT NULL DEFAULT nextval('id_seq'::regclass),
PRIMARY KEY ("id")
);
And I want to modify the sequence:
ALTER SEQUENCE ok_id_seq RESTART;
I keep getting errors at ok_id_seq.
I tried id_seq only
Tried quotes everywhere.
Ok, I now learnt that sequences are not table specific. I was trying to do alter table alter sequence.
I also used SELECT * FROM information_schema.sequences; to view all available sequences.
I don't know what happened so I recreated the table, checked for the sequence name.
Then was able to alter it with restart

Postgres ALTER TABLE ADD CONSTRAINT IF NOT EXISTS not working

I need to add a Constraint if not exists and am hitting the following error. Note that a similar if not exists for a new Column, right above it, does work. There's some syntax error when adding a Constraint, am I missing something?
alter table requests_t
add constraint if not exists
valid_bias_check CHECK (bias_flag::text = ANY (ARRAY['Y'::character varying::text, 'N'::character varying::text]));
Error
ERROR: syntax error at or near "not"
LINE 2: add constraint if not exists
Since Postgres doesn't support this syntax with constraints (see a_horse_with_no_name's comment), I rewrote it as:
alter table requests_t
drop constraint if exists valid_bias_check;
alter table requests_t
add constraint
valid_bias_check CHECK (bias_flag::text = ANY (ARRAY['Y'::character varying::text, 'N'::character varying::text]));

Facing error while Altering table in PostgreSQL

I was creating tables in PostgreSQL. Then I had to use an column as foreign key so I altered my table to define that column as foreign key.
But I got an error at "WITH",
ALTER TABLE Account WITH NOCHECK ADD CONSTRAINT FK_Account_AccountCPCMapping FOREIGN KEY(nAccountCPCMappingID)
REFERENCES AccountCPCMapping (nAccountCPCMappingID);
I am getting error like,
ERROR: syntax error at or near "WITH"
LINE 1: ALTER TABLE Account WITH NOCHECK ADD CONSTRAINT FK_Account...
Please suggest any corrections.
you try to use Microsoft sql server syntax https://learn.microsoft.com/en-us/sql/t-sql/statements/alter-table-transact-sql
while Postgres syntax https://www.postgresql.org/docs/current/static/sql-altertable.html is
...ADD table_constraint [ NOT VALID ]

DB2 -- unique constraint on multiple columns

I'm trying to add a unique constraint on 2 columns on DB2-- docType column and title column. The values of each column can repeat in themselves. however, the values of (docType, title) pairs should not repeat.
I tried so far
ALTER TABLE externalfiles
ADD CONSTRAINT logicalKey UNIQUE (doctype, title)
and
alter table externalfiles add unique (doctype, title)
, and got the following error to both:
Operation not allowed for reason code "7" on table "PIT.EXTERNALFILES".. SQLCODE=-668, SQLSTATE=57016, DRIVER=4.21.29
Isn't this any allowed??
DB2 Unique Constraint over multiple Columns suggests creating indexes as alternative to this. Haven't tried indexing them yet-- however, i'm wondering why the unique constraint isn't working.
TIA.
This web search on db2 luw sqlstate 57016 returns a number of links that suggest a prior ALTER may require that a REORG be performed before the ADD CONSTRAINT can be effected.
Perhaps most notable was the following doc link and snippet of text describing the RC7 [apparently SQLERRMC=7]:
DB2 for Linux UNIX and Windows 9.7.0
->Database fundamentals
->Messages
->SQL Messages
->SQL0500 - SQL0999
->SQL0668N
…
7 • The table is in the reorg pending state. This can occur after an
ALTER TABLE statement containing a REORG-recommended operation.
…
First try to reorganize table by
call sysproc.admin_cmd ('reorg table <TABLE_NAME>');
Than try again to create unique index. It should work.

Postgres: Error constraint "fk" of relation "tbl" does not exist (with Yii - PHP)

I searched for this problem. But my postgres user has enough grant and I do not think I have misspelling error. However I am newbie.
I have this error message:
21:38:03 set search_path='public'
21:38:03 ALTER TABLE public.tbl_user DROP CONSTRAINT "fk-user-access-user-id"
21:38:03 ERROR: constraint "fk-user-access-user-id" of relation "tbl_user" does not exist
I use the PhpStorm. I just open the database view, expanded the tbl_user table, right click and select "drop". And I got this error in the console.
So the above SQL command generated by the PhpStorm.
Then I tried with these commands manually on Ubuntu:
ALTER TABLE tbl_user DROP CONSTRAINT "fk-user-access-user-id"
ALTER TABLE "tbl_user" DROP CONSTRAINT "fk-user-access-user-id"
But I get the same error.
In the PhpStorm I see this definition:
"fk-user-access-user-id" FOREIGN KEY (access_id) REFERENCES tbl_access (id)
The tbl_access table exists with the primary id key.
I do not understand this error message, because the "fk-user-access-user-id" foreign key is at the tbl_user and so for me the 'relation "tbl_user" does not exist' strange. I do not understand.
I tried to find similar problem on StackOverflow, but I gave up after 20x question reading.
By the way, the postgres code was generated by the Yii framework.
$this->addColumn('{{%user}}', 'access_id', $this->integer()->notNull()->defaultValue(1)->after('status'));
$this->addForeignKey('fk-user-access-user-id', '{{%user}}', 'access_id', '{{%access}}', 'id');
first row mean add access_id column to the user table.
second row: create foreign key with 'fk-user...' name on tbl_user table's access_id column references to tbl_access table's id column.
So I used this PHP code to generate this SQL commands. I prefer this way because for me the migration files are very useful.
Most likely the definition and actual implementation in your underlying DB has changed from what the app has recorded. Depending on what the history is, either a change in the app for that foreign key relationship was not migrated to persist the change at the database level, or someone has executed some operation directly at the DB level to remove the relationship. You will need to sync up the app layer to the DB at this point I would think.