Quantcast
Channel: PostgreSQL - insert/update violates foreign key constraints - Database Administrators Stack Exchange
Viewing all articles
Browse latest Browse all 3

Answer by BartekR for PostgreSQL - insert/update violates foreign key constraints

$
0
0

It all depends on what do you want to do with the data.

First example - you want to have consistent data in all tables, but you try to insert values that doesn't match with Table1.

Second example - you don't want to have consistent data, but try to do something else, not exactly know what. Table can't have more than one primary key.

Third example - you still don't know what you want to achieve and put UNIQUE constraint on column that may have he same value multiple times.

If you just want to insert your data - get rid of foreign key references in first example. If you want to have consistent data in all tables - do the data cleanup and then insert in tables WITH foreign key constraints.

tl;dr: to insert your data into Table3 with code from first example - insert missing values into Table1.DataID column that exist in Table3.DataId.


Viewing all articles
Browse latest Browse all 3

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>