Skip to content

Chart of Accounts Framework Database Design

Erik Huelsmann edited this page Jan 12, 2020 · 2 revisions

Proposed table structure:

CREATE TABLE coa_node (
    id serial NOT NULL UNIQUE,
    parent int,
    number text PRIMARY KEY,
    description text NOT NULL
);
CREATE TABLE account_heading (like coa_node INCLUDING ALL) INHERITS (coa_node);
CREATE TABLE account (
     like coa_node INCLUDING ALL,
     contra bool default false,
     gifi 
) INHERITS (coa_node);
ALTER TABLE coa_node ADD CHECK NOINHERIT (FALSE);
ALTER TABLE account ALTER COLUMN parent set not null;

-- translations
CREATE TABLE coa_translation (
     coa_id int,
     language_code text,
     description text,
     PRIMARY KEY (coa_id, language_code)
);

CREATE TABLE account_heading_translation ( 
   LIKE coa_translation INCLUDING ALL,
   FOREIGN KEY (coa_id) REFERENCES account_heading (id)
) INHERITS (coa_translation);

CREATE TABLE account_translation ( 
   LIKE coa_translation INCLUDING ALL,
   FOREIGN KEY (coa_id) REFERENCES account (id)
) INHERITS (coa_translation);

ALTER TABLE coa_translation ADD CHECK NOINHERIT (FALSE);

I'm loving the account structure with the nodes being a generalization of the headers/accounts. This needs some further explaining though:

-- contexts
CREATE TABLE account_context (
    id serial not null unique,
    module text,
    context text,
    primary key (module, context)
);
CREATE TABLE account_to_context (
    account_id int not null references account(id),
    context_id int not null references account_context(id)
);