无法启用约束。一行或多行包含违反非空,唯一或外键约束的值
我在 informix
数据库中建立外部连接并成功执行,但是我的代码中出现以下异常:
I make an outer join and executed successfully in the informix
database but I get the following exception in my code:
DataTable dt = TeachingLoadDAL.GetCoursesWithEvalState(i, bat);
无法启用约束。一行或多行包含违反非空,唯一或外键约束的值
。
Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.
我知道问题,但我不知道如何解决它。
I know the problem, but I don't know how to fix it.
我做外连接的第二个表包含复合主键,在前面的外连接查询。
The second table I make the outer join on contains a composite primary key which are null in the previous outer join query.
编辑:
SELECT UNIQUE a.crs_e, a.crs_e || '/ ' || a.crst crs_name, b.period,
b.crscls, c.crsday, c.from_lect, c.to_lect,
c.to_lect - c.from_lect + 1 Subtraction, c.lect_kind, e.eval, e.batch_no,
e.crsnum, e.lect_code, e.prof_course
FROM rlm1course a, rfc14crsgrp b, ckj1table c, mnltablelectev d,
OUTER(cc1assiscrseval e)
WHERE a.crsnum = b.crsnum
AND b.crsnum = c.crsnum
AND b.crscls = c.crscls
AND b.batch_no = c.batch_no
AND c.serial_key = d.serial_key
AND c.crsnum = e.crsnum
AND c.batch_no = e.batch_no
AND d.lect_code= e.lect_code
AND d.lect_code = ....
AND b.batch_no = ....
发生与表 cc1assiscrseval
。主键是(batch_no,crsnum,lect_code)。
The problem happens with the table cc1assiscrseval
. The primary key is (batch_no, crsnum, lect_code).
如何解决这个问题?
编辑:
根据 @PaulStock
建议:
我做他说的,我得到:
According to @PaulStock
advice:
I do what he said, and i get:
? dt.GetErrors()[0] {System.Data.DataRow} HasErrors:true ItemArray:
{object [10]} RowError:列'eval'不允许DBNull.Value。
? dt.GetErrors()[0] {System.Data.DataRow} HasErrors: true ItemArray: {object[10]} RowError: "Column 'eval' does not allow DBNull.Value."
所以我通过将 e.eval
替换为 NVL来解决我的问题(e.eval,'')eval
。这解决了我的问题。
So I solve my problem by replacing e.eval
to ,NVL (e.eval,'') eval
.and this solves my problem.
Thanks a lot.
这个问题通常是由以下之一引起的
This problem is usually caused by one of the following
- 未设置为AllowDBNull的列返回的空值
- 使用相同的主键返回的行重复。
- 数据库和数据集之间的列定义不匹配(例如,字符字段的大小)
您的查询本地并查看结果,如果结果集不是太大。如果你已经消除了空值,那么我的猜测是主键列被重复。
Try running your query natively and look at the results, if the resultset is not too large. If you've eliminated null values, then my guess is that the primary key columns is being duplicated.
或者,要查看确切的错误,您可以手动添加一个Try / Catch块生成的代码,这样,然后打破时,异常生成:
Or, to see the exact error, you can manually add a Try/Catch block to the generated code like so and then breaking when the exception is raised:
然后在命令窗口中,调用 GetErrors
方法在表上得到错误。
对于C#,命令将是? dataTable.GetErrors()
对于VB,命令是? dataTable.GetErrors
Then within the command window, call GetErrors
method on the table getting the error.
For C#, the command would be ? dataTable.GetErrors()
For VB, the command is ? dataTable.GetErrors
这将显示所有出现错误的数据行。你可以得到然后看看 RowError
为每一个,应该告诉你的列是无效的问题。所以,为了看到错误的第一个数据行错误的命令是:? dataTable.GetErrors(0).RowError
或在C#中它将是? dataTable.GetErrors()[0] .RowError
This will show you all datarows which have an error. You can get then look at the RowError
for each of these, which should tell you the column that's invalid along with the problem. So, to see the error of the first datarow in error the command is:? dataTable.GetErrors(0).RowError
or in C# it would be ? dataTable.GetErrors()[0].RowError