Facing error while Altering table in PostgreSQL - 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 ]

Related

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]));

PostgreSQL Error: insert or update on table violates foreign key constraint

I have a foreign key constraint on a table and when I’m inserting data I get the following error:
ERROR: insert or update on table "gl_account_item" violates foreign key constraint "fk_gl_account_id" DETAIL: Key (gl_account_id)=(939) is not present in table "gl_account". SQL state: 23503
…if I query the table I can clearly see that it is:
Here is the CREATE TABLE statement:
I cannot understand why I'm getting a foreign key constraint violation error when the id is clearly there in the primary key table. If I remove the foreign key constraint and insert the data and put the constraint back and run a query with a join on that field everything works and the data is there:
Please help.

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.

Postgresql User Defined Type in primary key constraint

I'm using postgresql 9.1 on Ubuntu 12.04. I have a user defined type as one column of a table. When I create a primary key constraint I get a syntax error.
Here is a sample sql script I'm using with psql to create the table:
CREATE TYPE my_type AS
(
field1 integer,
field2 integer
);
CREATE TABLE my_table
(
my_data my_type,
other_data integer
);
ALTER TABLE my_table ADD CONSTRAINT pk_my_table PRIMARY KEY (my_data.field1);
I get this error:
ERROR: syntax error at or near "."
LINE 1: ...ble ADD CONSTRAINT pk_my_table PRIMARY KEY (my_data.field1);
I've tried using (my_data).field1 but also get a syntax error.
If I just use my_data in the constraint there is no error:
ALTER TABLE my_table ADD CONSTRAINT pk_my_table PRIMARY KEY (my_data);
But I would like to use just one field as part of the constraint.
Thanks for any ideas.
I found this question using google and I'm pretty sure is dead but I still want to answer it cause someone else might see it.
Short answer is you have to use the field's name:
ALTER TABLE "public"."carga" ADD CONSTRAINT "pk_my_table" PRIMARY KEY ("field1");

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

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.