How to fix the ALTER TABLE statement conflicted with the FOREIGN KEY constraint - azure-sql-server-managed-instance

Unable to create a relationship 'FK_Sales_Date'.
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_Sales_Date". The conflict occurred in database "SalesDataWarehouse", table "dbo.Date", column 'DateKey'.

Related

Getting an error while assigning FK's to tables in postGIS?

I have created 5 tables, with four of them having a Primary Key. I then try to assign Foreign Keys using the following alter statement:
ALTER TABLE SensorLocation
ADD CONSTRAINT constraint_name
FOREIGN KEY (sensor_id)
REFERENCES Sensor (sensor_id)
;
ALTER TABLE DataStream
ADD CONSTRAINT constraint_name
FOREIGN KEY (sensorlocation_id)
REFERENCES SensorLocation (sensorlocation_id)
;
ALTER TABLE DataStream
ADD CONSTRAINT constraint_name
FOREIGN KEY (property_id)
REFERENCES ObservedProperty (property_id)
;
ALTER TABLE Observation
ADD CONSTRAINT constraint_name
FOREIGN KEY (observation_id)
REFERENCES DataStream (observation_id)
;
The thing is, when I only use one constraint per table, it works. But when I want to assign multiple to the table DataStream it doesn't work.
The error message I receive is as follows:
constraint "constraint_name" for relation "datastream" already exists
Does anyone have any idea on how to solve this? All help is welcome!
This is our data-structure:

Query tool response pgAdmin

ALTER TABLE ONLY rental ADD CONSTRAINT rental_customer_id_fkey FOREIGN KEY (customer_id) REFERENCES customer(customer_id) ON UPDATE CASCADE ON DELETE RESTRICT;
ERROR: there is no unique constraint matching given keys for referenced table "customer"
ALTER TABLE ONLY customer ADD CONSTRAINT customer_store_id_fkey FOREIGN KEY (store_id) REFERENCES store(store_id) ON UPDATE CASCADE ON DELETE RESTRICT;
ALTER TABLE ONLY film ADD CONSTRAINT film_language_id_fkey FOREIGN KEY (language_id) REFERENCES language(language_id) ON UPDATE CASCADE ON DELETE RESTRICT;
ALTER TABLE ONLY inventory ADD CONSTRAINT inventory_store_id_fkey FOREIGN KEY (store_id) REFERENCES store(store_id) ON UPDATE CASCADE ON DELETE RESTRICT;
ERROR: insert or update on table "inventory" violates foreign key constraint
I get these messages when i use the query tool for a sql query of over 1300 lines of code. I'm still learning so any pointers will be helpful

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.

Foreign key issue while insert or delete

i have 3 tables below
books
users
and
test.users_books with foreign keys
CREATE TABLE test.users_books (
user_id int NOT NULL,
PRIMARY KEY (user_id),
FOREIGN KEY (user_id) REFERENCES test.users(id) ON UPDATE CASCADE ON DELETE CASCADE,
FOREIGN KEY (user_id) REFERENCES test.books(id) ON UPDATE CASCADE ON DELETE CASCADE
);
while am trying to insert in to
INSERT INTO test.users_books values(11)
getting below error
ERROR: insert or update on table "users_books" violates foreign key constraint "users_books_user_id_fkey"
DETAIL: Key (user_id)=(11) is not present in table "users".
********** Error **********
ERROR: insert or update on table "users_books" violates foreign key constraint "users_books_user_id_fkey"
SQL state: 23503
Detail: Key (user_id)=(11) is not present in table "users".
how to insert 11 into this table with foreign key the id 11 having in books table.
is there any condition need insert for this table please let meknow

postgres key is not present in table constraint

When trying to ALTER TABLE in Postgres 9.5 to create foreign key constraint: from product_template.product_brand_id to product_brand.id
ALTER TABLE public.product_template
ADD CONSTRAINT product_template_product_brand_id_fkey
FOREIGN KEY (product_brand_id)
REFERENCES public.product_brand (id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE SET NULL;
Returns error
ERROR: insert or update on table "product_template" violates foreign key constraint "product_template_product_brand_id_fkey"
DETAIL: Key (product_brand_id)=(12) is not present in table "product_brand".
STATEMENT: ALTER TABLE "product_template" ADD FOREIGN KEY ("product_brand_id") REFERENCES "product_brand" ON DELETE set null
Im confused why postgres is trying to find product_brand.product_brand_id, when the fkey is from product_template.product_brand_id to product_brand.id
Any ideas?
The error message simply states that there is at least one row in the table product_template that contains the value 12 in the column product_brand_id
But there is no corresponding row in the table product_brand where the column id contains the value 12
Key (product_brand_id)=(12) relates the source column of the foreign key, not the target column.
In simple terms, the value of FOREIGN KEY(product_brand_id) provided in your ALTER statement is not present in the source (product_brand) table.