using the data modeller embedded in sql developer 19.4 against an oracle 12c database.
I added a couple of foreign keys within the data modeller erd diagram then I clicked the "syncrhonize data modeller with model" ; then I noticed that in the generated DDL other than my legitimate changes the DDL contain also other SQL relative to changes already existing in the database; anyway I amended the DDL by deleting the unwanted changes and applied and commit my changes. then I run the same compare again and the "Compare Models" window correctly show no changes but... the DDL has the same shanges already applied and the ones that already exists in the database.
I also reverted the comparison by comparing the model with the erd and hit "merge" thinking that this is a problem with some kind of cahced memory etc.. but same issue here: "Compare Models" view does correctly show no chaages but DDL contain changes ??
and below the DDL with the script of changes that already exists in the database.
ALTER TABLE gasgendev.audit_errors
ADD CONSTRAINT audit_errors_look_audit_types_fk FOREIGN KEY ( audit_type )
REFERENCES gasgendev.look_audit_types ( audit_type_id )
ON DELETE CASCADE
NOT DEFERRABLE ENABLE VALIDATE;
ALTER TABLE gasgendev.audit_logs
ADD CONSTRAINT audit_logs_look_audit_types_fk FOREIGN KEY ( audit_type )
REFERENCES gasgendev.look_audit_types ( audit_type_id )
ON DELETE CASCADE
NOT DEFERRABLE ENABLE VALIDATE;
ALTER TABLE gasgendev.halo_inputs
ADD CONSTRAINT halo_inputs_look_assets_fk FOREIGN KEY ( look_assets_l_asset_id )
REFERENCES gasgendev.look_assets ( l_asset_id )
ON DELETE CASCADE
NOT DEFERRABLE ENABLE VALIDATE;
ALTER TABLE gasgendev.halo_inputs
ADD CONSTRAINT halo_inputs_look_datasets_fk FOREIGN KEY ( dataset_id )
REFERENCES gasgendev.look_datasets ( l_dataset_id )
ON DELETE CASCADE
NOT DEFERRABLE ENABLE VALIDATE;
ALTER TABLE gasgendev.manual_inputs
ADD CONSTRAINT manual_inputs_look_manual_inputs_fk FOREIGN KEY ( look_manual_inputs_look_manual_input_id )
REFERENCES gasgendev.look_manual_inputs ( look_manual_input_id )
ON DELETE CASCADE
NOT DEFERRABLE ENABLE VALIDATE;
ALTER TABLE gasgendev.manual_inputs
ADD CONSTRAINT manual_inputs_look_datasets_fk FOREIGN KEY ( dataset_id )
REFERENCES gasgendev.look_datasets ( l_dataset_id )
ON DELETE CASCADE
NOT DEFERRABLE ENABLE VALIDATE;
Of course if I run that script in SQL developer I get errors stating that those constraints already exists.
Can anyone tell me what i am doing wrong here?
This error means that either your Data Modeller or your SQL Dev are out of date, causing them to not cooperate. The constraints are there, but your DM isn't recognizing them. Update your system, and if that doesn't work, reinstall each manually. I've seen this happen once before, and it wasn't pretty. Hope this fixes your problem!
Related
I have a table with a foreign key reference and I had added a on_delete_cascade condition with that foreign key.
I don't need the rows to be deleted even if the foreign key object gets deleted.
How can I change the drop condition without have to drop the column?
Just drop the conatraint and then add it back without the ON DELETE CASCADE clause:
ALTER TABLE some_table DROP CONSTRAINT some_key,
ADD CONSTRAINT some_key FOREIGN KEY (id) REFERENCES tab(a_id);
Check what the real experts wrote by reading here:
https://www.postgresql.org/message-id/CABvLTWHdT0tTygV0-O_ZgLRRAGZAg0W4zvghfF2PshAzvkAaGg%40mail.gmail.com
I have a table called "Reviews" and it references a record in a table "ReviewSetups". When I delete a ReviewSetup I was to also delete all child Reviews (so cascade delete).
I have setup the foreign key like below on the Reviews table but nothing gets deleted when I delete a parent ReviewSetup.
I have other entities in by db as well which I migrated with a FK in exactly the same way and those work fine.
Does anyone have an idea what is going on here?
EDIT
Here's the code:
-- Foreign Key: "FK_Reviews_ReviewSetup_Id_ReviewSetups_Id"
-- ALTER TABLE "Reviews" DROP CONSTRAINT "FK_Reviews_ReviewSetup_Id_ReviewSetups_Id";
ALTER TABLE "Reviews"
ADD CONSTRAINT "FK_Reviews_ReviewSetup_Id_ReviewSetups_Id" FOREIGN KEY ("ReviewSetup_Id")
REFERENCES "ReviewSetups" ("Id") MATCH SIMPLE
ON UPDATE CASCADE ON DELETE CASCADE;
If you had to drop database again and again, it's better to disable constraints till you find the right culprit or re-design the schema.
Disable constraints and delete data, then re-enable again.
Disable constraints :
Alter table tablename NOCHECK CONSTRAINT constraintname
Enable again:
Alter table tablename CHECK CONSTRAINT constraintname
Ended up dropping the entire db and re-running the migration from scratch. Somehow that solved it. Somewhere, somehow the config was off a bit. Really curious what was the culprit though...
HI I'm new to postgresql environment so been lost for a while. I want to keep my data when parent entity is deleted. I want to know how to make 'ON DELETE SET NULL' for postgresql database. Please give me a clue.
ON DELETE SET NULL is a standard foreign key constraint option.
CREATE TABLE some_child (
parent_id integer references parent(id) on delete set null
);
or:
ALTER TABLE some_child
ADD CONSTRAINT parent_id_fk
FOREIGN KEY (parent_id) REFERENCES parent(id)
ON DELETE SET NULL;
See the documentation.
In future posts make sure you include your PostgreSQL version and explain what you've already tried.
I have run into problems when restoring a PostgreSQL database schema in another server. More precisely, some of the tables don't seem to have the same foreign key constraints associated with them that they used to in the original database. For example, the ON DELETE CASCADE clause seems to have completely evaporated from all of the constraint definitions.
That's probably because the dumping procedure didn't backup the ON DELETE CASCADE clauses in your table definitions.
Firstly you should delete the foreign key constraints on your tables and then go on to altering them:
Something like the following:
ALTER TABLE ONLY *your_table* DROP CONSTRAINT your_constraint;
After that, recreate the constraints with something like:
ALTER TABLE ONLY your_table ADD CONSTRAINT your_constraint (...ON DELETE CASCADE, etc..);
In my database, I have a number of objects that can be related to each other.
This is fine, until I decide I want to delete these objects. Because of the relation record, I need to implement cascade delete to prevent an exception from being thrown.
When an object that is on either side of the relation is deleted, I want the relation record to be deleted too. I would like to create a database structure that looks like this:
CREATE TABLE [MyObject]
(
[ID] [int] IDENTITY PRIMARY KEY,
...
);
CREATE TABLE [MyObjectRelation]
(
[ID] [int] IDENTITY PRIMARY KEY,
[MyObjectID] [int] FOREIGN KEY REFERENCES [MyObject] ([ID]) ON DELETE CASCADE,
[RelatedMyObjectID] [int] FOREIGN KEY REFERENCES [MyObject] ([ID]) ON DELETE CASCADE
)
However, whenever I attempt to run this on my database, I receive this error message:
Introducing FOREIGN KEY constraint '...' on table 'MyObjectRelation' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
I have read the documentation about this, but I do not see how this layout could cause a cycle. It is entirely possible of course, that I have misinterpreted the documentation on MSDN for cascade delete, and the database layout above will not achieve what I want here.
I would be very interested in hearing what I can do to implement the behaviour that I want.
You receive this error message because in SQL Server, a table cannot appear more than one time in a list of all the cascading referential actions that are started by either a DELETE or an UPDATE statement. For example, the tree of cascading referential actions must only have one path to a particular table on the cascading referential actions tree.
You can use triggers to achieve the same behavior.