即使数据确实存在,也会触发外键约束

时间:2017-08-14 10:18:49

标签: sql sql-server tsql

我有两个表E/AndroidRuntime: FATAL EXCEPTION: main Process: be.kdg.examen, PID: 4451 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{be.kdg.examen/be.kdg.examen.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "be.kdg.examen.MainActivity" on path: DexPathList[[zip file "/data/app/be.kdg.examen-1/base.apk"],nativeLibraryDirectories=[/data/app/be.kdg.examen-1/lib/x86, /system/lib, /vendor/lib]] at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2567) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) Caused by: java.lang.ClassNotFoundException: Didn't find class "be.kdg.examen.MainActivity" on path: DexPathList[[zip file "/data/app/be.kdg.examen-1/base.apk"],nativeLibraryDirectories=[/data/app/be.kdg.examen-1/lib/x86, /system/lib, /vendor/lib]] at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56) at java.lang.ClassLoader.loadClass(ClassLoader.java:380) at java.lang.ClassLoader.loadClass(ClassLoader.java:312) at android.app.Instrumentation.newActivity(Instrumentation.java:1078) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2557) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)  at android.app.ActivityThread.-wrap12(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:154)  at android.app.ActivityThread.main(ActivityThread.java:6119)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)  INTRFCTRL,两个都有IDENTITY列INTERROR

INTERROR附加了以下外键

FILEID

我将临时表中的数据插入ALTER TABLE [dbo].[INTERROR] WITH CHECK ADD CONSTRAINT [FK_INTERROR_0] FOREIGN KEY([FILEID]) REFERENCES [dbo].[INTRFCTRL] ([FILEID]) ON DELETE CASCADE GO ALTER TABLE [dbo].[INTERROR] CHECK CONSTRAINT [FK_INTERROR_0] ,然后尝试对INTRFCTRL执行相同操作并使用DBCC RESEED控制标识列值

我成功运行,可以看到INTRFCTRL表中存在67001的FILEID值

INTERROR

然后我尝试运行

DBCC CHECKIDENT (INTRFCTRL, RESEED, 67000)
INSERT INTO dbo.INTRFCTRL SELECT FILEHASH,CASENO,INTNO,FILENAM,... 
FROM INTRFCTRL_TEMP

我得到了

  

INSERT语句与FOREIGN KEY约束冲突   “FK_INTERROR_0”。冲突发生在数据库“ESSDEV”,表中   “dbo.INTRFCTRL”,列'FILEID'。

然而,我可以看到INTRFCTRL中的数据

我做错了什么?

2 个答案:

答案 0 :(得分:2)

我建议使用OUTPUT子句而不是操纵IDENTITY值:

  

我将临时表中的数据插入到INTRFCTRL中,然后尝试将其转换为INTERROR

DECLARE @MyTableVar table(identity_col INT, file_id INT );

INSERT INTO dbo.INTRFCTRL(col_list, ...)
OUTPUT inserted.identity_col, inserted.file_id  --get inserted identity value
INTO @MyTabVar
SELECT FILEHASH,CASENO,INTNO,FILENAM,... 
FROM INTRFCTRL_TEMP;

SET IDENTITY_INSERT dbo.INTERROR ON;

INSERT INTO dbo.INTERROR(col_list, ...)
SELECT t.identity_col, INTRFLINENO,CASENO... 
FROM INTERROR_TEMP it
JOIN @MyTabVar t
  ON it.fileId = t.File_id;

SET IDENTITY_INSERT dbo.INTERROR OFF;

答案 1 :(得分:0)

尝试在INTERROR上设置IDENTITY_INSERT OFF并从INTRFCTRL表中获取FILEID值。