Click here to Skip to main content
15,792,510 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have two table A and B. A table has fk_B_id and I want to insert data to A Table but B table is empty. When I insert to A table postgresql shows me that null value in column violates not-null constraint. is there any way where I insert data to table like force ??

What I have tried:

I tried to insert data with transaction but couldn't work.
Updated 22-Sep-22 0:36am

1 solution

No. If the column doesn't allow nulls, and has a foreign-key relationship to another table, then you have to insert a value into that column, and it has to match the primary key of a record in the foreign table.

If you want to allow nulls, then you need to change the properties of the column to allow nulls.

If you want to allow values which don't exist in the foreign table, then you need to remove the foreign key relationship.

Otherwise, you need to insert the record into the foreign table before inserting the record into the related table. You should be able to perform both inserts within a single transaction, so that they either both succeed or both fail.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900