2

我想在 PHP 中创建一个小型 ORM,它可以基于 JSON 结构更新数据库模式。为此,我使用 PHPs firebird PDO 驱动程序。我可以毫无问题地创建表、ID 生成器和触发器来增加 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)
4

1 回答 1

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;

这给了我错误。

于 2013-09-17T10:38:36.427 回答