Firebird上的外键问题

时间:2013-09-17 07:27:51

标签: php sql database foreign-keys firebird

我想在PHP中创建一个小的ORM,它可以根据JSON结构更新数据库模式。我使用PHPs firebird PDO驱动程序。我可以创建表,ID生成器和触发器到没有问题的incement ID,我可以将一个表链接到另一个或其他方式,但是当我尝试链接它们时我得到一般错误。

所以我创建了两个表

Users ( user_id, name, pwd, group_id )
Groups ( group_id, name, admin_id )

然后我尝试用ALTER TABLE ADD FOREIGN KEY语法链接它们。 users.group_id引用groups.group_idgroups.admin_id引用users.user_id。如上所述,当我将外键约束添加到任一表时,它都有效,但是当我尝试将约束添加到两个表时。它以通用错误code -902: internal consistency check failed.

退出

任何建议表示赞赏。

编辑:完整脚本

CREATE TABLE institutions ( institution_id int not null primary key, name VARCHAR(100), url VARCHAR(100), admin_id int );
CREATE GENERATOR gen_institution_id;
SET GENERATOR gen_institution_id to 1;
CREATE TRIGGER institutions_BI FOR institutions         BEFORE INSERT AS         BEGIN           if (NEW.institution_id is NULL) then NEW.institution_id = GEN_ID(GEN_institutions_ID, 1);         END;

CREATE TABLE privileges ( privilege_id int not null primary key, name VARCHAR(50), read SMALLINT, write SMALLINT, edit_own SMALLINT, edit_ins SMALLINT, edit_all SMALLINT, delete_own SMALLINT, delete_ins SMALLINT, delete_all SMALLINT, comment SMALLINT );
CREATE GENERATOR gen_privilege_id;
SET GENERATOR gen_privilege_id to 1;
CREATE TRIGGER privileges_BI FOR privileges         BEFORE INSERT AS         BEGIN           if (NEW.privilege_id is NULL) then NEW.privilege_id = GEN_ID(GEN_privileges_ID, 1);         END;

CREATE TABLE users ( user_id int not null primary key, firstName VARCHAR(60), name VARCHAR(60) NOT NULL, email VARCHAR(200) NOT NULL UNIQUE, password VARCHAR(60) NOT NULL, institution_id int, privilege_id int );
CREATE GENERATOR gen_user_id;
SET GENERATOR gen_user_id to 1;
CREATE TRIGGER users_BI FOR users         BEFORE INSERT AS         BEGIN           if (NEW.user_id is NULL) then NEW.user_id = GEN_ID(GEN_users_ID, 1);         END;
alter table institutions add foreign key (admin_id) references users(user_id);
alter table users add foreign key (institution_id) references institutions(institution_id), add foreign key (privilege_id) references privileges(privilege_id);

这给了我以下错误

SQLSTATE[HY000]: General error: -902 internal Firebird consistency check (can't continue after bugcheck)

1 个答案:

答案 0 :(得分:2)

好吧,似乎我的触发器定义是罪魁祸首。以下架构消除了错误消息:

CREATE TRIGGER BI_privileges_privilege_id FOR privileges 
ACTIVE BEFORE INSERT 
POSITION 0 
AS 
BEGIN 
  IF (NEW.privilege_id is NULL) 
  THEN NEW.privilege_id = GEN_ID(GEN_privilege_id, 1); 
END;

而不是:

CREATE TRIGGER privileges_BI FOR privileges
BEFORE INSERT 
AS
BEGIN 
  if (NEW.privilege_id is NULL) then 
  NEW.privilege_id = GEN_ID(GEN_privileges_ID, 1);         
END;

给了我错误。