martes, 23 de abril de 2013

Solucionar problemas de referencia circular en SQL Server


Algunas veces los errores más enrevesados tienen la solución más sencilla, este es un caso que puede tenernos horas parados si no nos damos cuenta pensando que no hay forma de romper la referencia circular de una tabla sobre otra, mucho cuidado porque si en vez de en  dos tablas se da entre varias puede tenernos entretenidos mucho tiempo.
Hacemos este Update:

update exportacion_paises_relacion set CodigoExportacion = 'ot' where CodigoExportacion = 'zz'

y nos da este error:

Mens. 547, Nivel 16, Estado 0, Línea 1
The UPDATE statement conflicted with the FOREIGN KEY constraint "FK__Exportaci__Codig__7A16EC61". The conflict occurred in database "dbPrueba", table "Exportacion_Paises", column 'Codigo'.

Buscamos la tabla de la foreign key y tratamos de solucionarlo

update exportacion_paises set Codigo = 'ot' where Codigo = 'zz'

Pero al ejecutar obtenemos otro error:

Mens. 547, Nivel 16, Estado 0, Línea 1
The UPDATE statement conflicted with the REFERENCE constraint "FK__Exportaci__Codig__7A16EC61". The conflict occurred in database "dbPrueba", table "Exportacion_Paises_Relacion", column 'CodigoExportacion'.

Comprobamos con horror que ahora el problema está en la primera tabla, de tal forma que si hacemos primero un update nos da error una tabla y si hacemos primero el otro nos da error la otra.

update exportacion_paises_relacion set CodigoExportacion = 'ot' where CodigoExportacion = 'zz'
update exportacion_paises set Codigo = 'ot' where Codigo = 'zz'
Mens. 547, Nivel 16, Estado 0, Línea 1

The UPDATE statement conflicted with the FOREIGN KEY constraint "FK__Exportaci__Codig__7A16EC61". The conflict occurred in database "dbPrueba", table "Exportacion_Paises", column 'Codigo'.
The statement has been terminated.

Mens. 547, Nivel 16, Estado 0, Línea 2
The UPDATE statement conflicted with the REFERENCE constraint "FK__Exportaci__Codig__7A16EC61". The conflict occurred in database "dbPrueba", table "Exportacion_Paises_Relacion", column 'CodigoExportacion'.
The statement has been terminated.

Y si lo intentamos del revés nos da los mismos errores pero en orden inverso.

update exportacion_paises set Codigo = 'ot' where Codigo = 'zz'
update exportacion_paises_relacion set CodigoExportacion = 'ot' where CodigoExportacion = 'zz'

Mens. 547, Nivel 16, Estado 0, Línea 2
The UPDATE statement conflicted with the REFERENCE constraint "FK__Exportaci__Codig__7A16EC61". The conflict occurred in database "dbPrueba", table "Exportacion_Paises_Relacion", column 'CodigoExportacion'.
The statement has been terminated.

Mens. 547, Nivel 16, Estado 0, Línea 1
The UPDATE statement conflicted with the FOREIGN KEY constraint "FK__Exportaci__Codig__7A16EC61". The conflict occurred in database "dbPrueba", table "Exportacion_Paises", column 'Codigo'.
The statement has been terminated.

La solución es muy sencilla, basta con darse cuenta de que el código que intentamos modificar NO EXISTE en la tabla de códigos, en este caso exportacion_paises por tanto siempre dará error. Basta con darlo de alta, hacer el update correspondiente en la otra tabla y eliminar el código antiguo si fuera necesario.

insert into exportacion_paises values ('OT','Desconocido')
update exportacion_paises_relacion set CodigoExportacion = 'ot' where CodigoExportacion = 'zz'
delete exportacion_paises where Codigo = 'zz' 

  

No hay comentarios:

Publicar un comentario