2015-02-23 132 views
0

我正忙于编写一个脚本来恢复数据库备份,我遇到了一些奇怪的事情。PostgreSQL唯一索引错误

我有一个table.sql文件仅包含创建的表结构等

create table ugroups 
    (
    ug_code char(10) not null , 
    ug_desc char(60) not null 
); 

我有一个第二data.csv文件只包含定界符数据如

xyz | dummy data 
abc | more nothing 
fun | is what this is 

然后,我有第三个index.sql文件,它只创建索引

create unique index i_ugroups on ugroups 
    (ug_code); 

我使用命令终端像这样

/opt/postgresql/bin/psql -d dbname -c "\i /tmp/table.sql" # loads table.sql 

我有一个批处理脚本,加载完美的数据。然后我用命令

/opt/postgresql/bin/psql -d dbname -c "\i /tmp/index.sql" # loads index.sql 

当我尝试创建唯一索引这是给我的错误

ERROR: could not create unique index "i_ugroups" 
DETAIL: Key (ug_code)=(transfers) is duplicated. 

什么奇怪的是,当我执行table.sql文件和index.sql一起文件并加载数据我没有得到任何错误,它的一切工作。

有什么我失踪?为什么它不会让我在数据加载后创建唯一索引?

+1

也许你在创建索引之前意外加载了两次数据? – alexius 2015-02-23 11:37:34

回答

0

您的列ug_code中有两行与数据“转移”,这就是为什么它不能创建索引。

为什么它会成功,如果你先创建索引,我不知道。但是我怀疑它第二次尝试将“传输”插入到数据库中时,它只是不及格insert,而其他数据则成功插入。