FK constraint question

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

FK constraint question

Heinemann, Manfred (IMS)

Can something explain why something like the following could happen?

 

INSERT INTO task_history (task_id, user_id, task_date)

SELECT task_id, user_id, task_date

FROM task

WHERE task_id IN (1, 2, 3);

 

ERROR: insert or update on table "task_history" violates foreign key constraint "fk_task_history_task_id"

  Detail: Key (task_id)=(1) is not present in table "task".

 

Thanks,

Manfred




Information in this e-mail may be confidential. It is intended only for the addressee(s) identified above. If you are not the addressee(s), or an employee or agent of the addressee(s), please note that any dissemination, distribution, or copying of this communication is strictly prohibited. If you have received this e-mail in error, please notify the sender of the error.
Reply | Threaded
Open this post in threaded view
|

Re: FK constraint question

Steve Midgley-3
On Wed, Apr 1, 2020 at 6:49 AM Heinemann, Manfred (IMS) <[hidden email]> wrote:

Can something explain why something like the following could happen?

 

INSERT INTO task_history (task_id, user_id, task_date)

SELECT task_id, user_id, task_date

FROM task

WHERE task_id IN (1, 2, 3);

 

ERROR: insert or update on table "task_history" violates foreign key constraint "fk_task_history_task_id"

  Detail: Key (task_id)=(1) is not present in table "task".

 

Would you share the DDL create table and create FK statements? Seems weird for sure, but it would help to have the DDL to diagnose. Also, if you can provide sample data that generates this error, that will help everyone interested to reproduce your issue.

Reply | Threaded
Open this post in threaded view
|

Re: FK constraint question

Samed YILDIRIM
In reply to this post by Heinemann, Manfred (IMS)
Hello Manfred,
 
Did the table named task have child tables? The records whose task_id was 1 were in child tables rather than task?
 
Could you please try following query?
 

INSERT INTO task_history (task_id, user_id, task_date)

SELECT task_id, user_id, task_date

FROM only task

WHERE task_id IN (1, 2, 3);

Best regards.
Samed YILDIRIM
 
 
 
01.04.2020, 16:49, "Heinemann, Manfred (IMS)" <[hidden email]>:

Can something explain why something like the following could happen?

 

INSERT INTO task_history (task_id, user_id, task_date)

SELECT task_id, user_id, task_date

FROM task

WHERE task_id IN (1, 2, 3);

 

ERROR: insert or update on table "task_history" violates foreign key constraint "fk_task_history_task_id"

  Detail: Key (task_id)=(1) is not present in table "task".

 

Thanks,

Manfred

 

Information in this e-mail may be confidential. It is intended only for the addressee(s) identified above. If you are not the addressee(s), or an employee or agent of the addressee(s), please note that any dissemination, distribution, or copying of this communication is strictly prohibited. If you have received this e-mail in error, please notify the sender of the error.
Reply | Threaded
Open this post in threaded view
|

Re: FK constraint question

Ashutosh Sharma
In reply to this post by Heinemann, Manfred (IMS)
Hi,

Do you have multiple task table created in your database? 

Can you share the output of \d+ task_history?

On Wednesday, April 1, 2020, Heinemann, Manfred (IMS) <[hidden email]> wrote:

Can something explain why something like the following could happen?

 

INSERT INTO task_history (task_id, user_id, task_date)

SELECT task_id, user_id, task_date

FROM task

WHERE task_id IN (1, 2, 3);

 

ERROR: insert or update on table "task_history" violates foreign key constraint "fk_task_history_task_id"

  Detail: Key (task_id)=(1) is not present in table "task".

 

Thanks,

Manfred




Information in this e-mail may be confidential. It is intended only for the addressee(s) identified above. If you are not the addressee(s), or an employee or agent of the addressee(s), please note that any dissemination, distribution, or copying of this communication is strictly prohibited. If you have received this e-mail in error, please notify the sender of the error.