Matt Rajkowski wrote:
This came about by merging a few concepts...
The PermissionCategory table reflects the modules in the system, but the CustomFieldCategory reflects the grouping of a series of custom fields... both using category_id in a different way (not sure which came first).
Ok.. I guess most users of CentricCRM never look at the table structures...<G>
What I do suggest though is for us "data geeks" that this one script have a few more "notes" in it... My first look at it and "red flags" went off like fireworks...<G>
I'm not sure how to resolve the creation of [custom_field_category] as it references module_field_categorylink(category_id) which is not the primary key, but is also unique.
I found that the following worked..
CREATE TABLE module_field_categorylink (
id INTEGER NOT NULL,
module_id INTEGER NOT NULL REFERENCES permission_category(category_id),
category_id INTEGER NOT NULL UNIQUE ,
..<snip>...
PRIMARY KEY (ID, MODULE_ID, CATEGORY_ID)
);
This creates a different "data structure" because it will allow for mulitiple combinations of "id/module_id/category_id".. whereas having only the "id" as the primary key insures that there is only one record with that "id" This might be risky, "unless" the application code carefully restricts this..
If we remove this 1 reference here in [custom_field_category], then I don't see any application issue.
Maybe the rest of the custom_field tables will work?
Well actually no..
We encounter the same problem between "custom_field_category" and "custom_field_group".. and then again between "custom_field_group" and "custom_field_info".. and etc down to "custom_field_record". Each time the FK requirement is on a field that is not part of a Unique primary key field (which as I understand relational theory, has to be both Unique AND part of the primary key to be a FK) Firebird is more "anal" about adhereing to this model.. hence the problem..
I got these four tables to work with the following structure.. (snipped out the extraneous fields for brevity..):
CREATE TABLE module_field_categorylink (
id INTEGER NOT NULL,
module_id INTEGER NOT NULL REFERENCES permission_category(category_id),
category_id INTEGER NOT NULL UNIQUE ,
<snip>
PRIMARY KEY (ID, MODULE_ID, CATEGORY_ID)
);
CREATE TABLE custom_field_category (
module_id INTEGER NOT NULL REFERENCES module_field_categorylink(category_id),
category_id INTEGER NOT NULL UNIQUE,
<snip>
PRIMARY KEY (MODULE_ID, CATEGORY_ID)
);
CREATE TABLE custom_field_group (
category_id INTEGER NOT NULL REFERENCES custom_field_category(category_id),
group_id INTEGER NOT NULL UNIQUE,
<snip>
PRIMARY KEY (CATEGORY_ID, GROUP_ID)
);
CREATE TABLE custom_field_info (
group_id INTEGER NOT NULL REFERENCES custom_field_group(group_id),
field_id INTEGER NOT NULL UNIQUE,
<snip>
PRIMARY KEY (GROUP_ID, FIELD_ID)
);
CREATE TABLE custom_field_lookup (
field_id INTEGER NOT NULL REFERENCES custom_field_info(field_id),
code INTEGER NOT NULL,
<snip>
PRIMARY KEY (FIELD_ID, CODE)
);
CREATE TABLE custom_field_record (
link_module_id INTEGER NOT NULL,
link_item_id INTEGER NOT NULL,
category_id INTEGER NOT NULL REFERENCES custom_field_category(category_id),
record_id INTEGER NOT NULL UNIQUE,
<snip>
PRIMARY KEY (LINK_MODULE_ID, LINK_ITEM_ID, CATEGORY_ID, RECORD_ID)
);
======
The question is: Though this is more relationally correct, Would this work "application" wise...???
Let me know what you think...
John..
PS.. down to the last 5 scripts....