too long

I want to create a tiny ORM in PHP which can update the database schema based on an JSON structure. I use PHPs firebird PDO driver for that. I can create Tables, ID generators and Triggers to incement IDs without problems, I can link one table to the other or other way around, but i get an generic error when i try to link them both.

So I have created two tables

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

then i try to link them with an ALTER TABLE ADD FOREIGN KEY syntax. users.group_id references groups.group_id, and groups.admin_id references users.user_id. As mentioned, it works when I add the foreign key constraint to either table, but not when i try to add the constraints to both. it exits with an generic error code -902: internal consistency check failed.

Any advice appreciated.

EDIT: full script

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);

this gives me following error

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

okay, it seems my trigger definitions were the culprit. following schema eliminated the error message:

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;

as opposed to:

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;

which gave me the error.