Is it possible to raise a custom message in SQL Server when trying to delete an entity with a foreign key violation? I use EntityFramework 6 and I would like to have a custom SqlException raised by sql server to better specify the error. Today EntityFramework raises the exception caused by sql server and I don't want to parse the general sql exception messages to specify such issues.
This is the exception:
System.Exception: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.SqlClient.SqlException: The MERGE statement conflicted with the REFERENCE constraint "FK_My_ForeignKey"....
I think about something like this:
...
CONSTRAINT [FK_My_ForeignKey] FOREIGN KEY([Code]) REFERENCES [dbo].[MyTable] ([Code]) ON DELETE No Action AND RAISEERROR('MyCustomFKViolationError')
...
Or is there a better way to handle this via the `EntityFramework?
Changing this behavior in SQL Server would be a little strange. You can overcome this problem simply in .Net/Entity Framework. Just look at this post to overcome it:
https://stackoverflow.com/a/2403361/1666800
Related
I'm using PostgreSQL for a school project and I'm trying to add different foreign keys to my database tables but when i run for example:
ALTER TABLE PARTSUPP ADD FOREIGN KEY (PS_SUPPKEY) REFERENCES SUPPLIER(S_SUPPKEY);
on the SQL shell I receive the following error message:
server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
The connection to the server was lost. Attempting reset: Failed.
I hope that you can help me with this problem!
Your PostgreSQL server is crashing when you run your statement. There may be more information available in the Postgres logs.
I have a Model class that has an attribute referencing another instance of the same Model class. Its basically a tree structure in one Model.
When I try to exectute MyModel.deleteAll() it fails because a foreign key constraint fails.
Is there someway to easily suspend this constraint for the deleteAll query?
The only workaround I've found, since I'm using mysql, is to issue a TRUNCATE statement, which mysql accepts straight away.
Thanks in advance,
Evan
Exception details:
org.javalite.activejdbc.DBException: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (visibledb_testing.accountabilities, CONSTRAINT accountabilities_prototype_id FOREIGN KEY (prototype_id) REFERENCES accountabilities (id)), query: DELETE FROM accountabilities
The Model#deleteAll() simply generates SQL DELETE FROM YOURTABLE.
If you can run this on MySQL console, it will work from the model. If you are getting constraint violation, maybe you want to:
Base.exec("TRUNCATE " + MyModel.getTableName());
Alternatively, you can try http://javalite.io/delete_cascade#method-deletecascade. BE CAREFUL- this is a powerful but dangerous method, only use after reading all docs.
On dropping a unique constraint, following error occurs,
ORA-04098: trigger 'SYS.XDB_PI_TRIG' is invalid and failed re-validation
Having no permission to recompile this trigger.
What could be the problem here and is there any way we can solve this?
This error reflects a compilation/authorization failure for a mentioned trigger. This trigger was invalid so failed to be retrieved for execution. You can run
SHOW ERRORS TRIGGER SYS.XDB_PI_TRIG;
in order to have a bigger picture of that error.
Trigger may need to be recompiled. Running:
alter trigger SYS.XDB_PI_TRIG compile
will recompile this trigger.
Common case is when user has privileges only to run and not change respective triggers. In that case you may need to recompile the Trigger as SYSDBA.
I wouldthink that you may be dropping a primary key check what constraint your dropping. if your dropping a pk and its being used as a foreign key then this would invalidate the trigger.
Found a solution to this,
The XDB Schema is invalid for the Database. So we are unable to drop any objects in this Database. So making the XDB schema valid, has solved this problem.
Thanks for your answers!
I would like to know is it possible to return a DB ERROR code from trigger, rather than Custom exception created with RAISE_APPLICATION_ERROR. I have some requirement where i need to return an DB ERROR code like 1 for Unique Constraint. I have a purpose to ask this, because for some reason with some hash based partition we are not creating unique constraints on table and rather have one trigher with which i want to return Unique constraint as an error code after checking if the record exists in DB.
You can use:
RAISE DUP_VAL_ON_INDEX;
Only in this case you can't pass any arguments to this error and you will get:
ORA-00001: unique constraint (.) violated
Any one please help me out. I am syncing sql server to client using MS Sync Framework. I am getting this exception at syncOrchestrator.Synchronize();
Failed to execute the command 'BulkInsertCommand' for table ‘xyz'; the transaction was rolled back. Ensure that the command syntax is correct.
Inner Exception is:
Violation of PRIMARY KEY constraint 'PK__#B1CF471__ECD9228532A93525'. Cannot insert duplicate key in object 'dbo.#changeTable'. The duplicate key value is (0). The data for table-valued parameter \"#changeTable\" doesn't conform to the table type of the parameter. The statement has been terminated.
There is a store procedure that using above object 'dbo.#changeTable', but I don't know what to do with that??