mysql - One to one relationship with foreign key to mutiple tables -
please let me start saying, know has been asked many times before , i've studied other questions (and answers) after 2 days of reading questions , amending database can't work want.
at moment have various tables, example customer
, supplier
, product
, banner
, etc.
i have table called custom_field
allows custom fields created , used against various other tables.
at moment of tables this:
general tables ============== customer +-------------+---------------+ | customer_id | customer_name | +-------------+---------------+ | 1 | peter | | 2 | sally | +-------------+---------------+ banner +-----------+-------------+--------------+ | banner_id | banner_name | banner_width | +-----------+-------------+--------------+ | 1 | easter | 100px | | 2 | xmas | 250px | +-----------+-------------+--------------+ tables managing custom fields ================================= custom_field +----------+------------+----------------+-----------+ | field_id | field_name | field_label | item_type | +----------+------------+----------------+-----------+ | 100 | fav_color | favorite color | customer | | 101 | border | border | banner | +----------+------------+----------------+-----------+ custom_field_value +----------+----------+---------+-------------+ | value_id | field_id | item_id | field_value | +----------+----------+---------+-------------+ | 1567 | 100 | 1 | red | | 1568 | 100 | 2 | blue | | 1569 | 101 | 1 | solid | | 1570 | 101 | 2 | dotted | +----------+----------+---------+-------------+
to clarify, item_id
refers customer_id
, banner_id
, or supplier_id
, etc. in example above means peter has "favorite color" custom field set red, , sally has "favorite color" custom field set blue.
the easter banner has "border" custom field set solid, , xmas banner has "border" custom field set dotted.
this works fine, except there can no foreign key or referential integrity set between custom_field_value.item_id
, customer.customer_id
(or banner.banner_id
) because item_id's context described item_type
field in custom_field
table.
i don't want create multiple nullable foreign keys (not sure work anyway) become unmanageable.
i did try creating sub tables, example customer_custom_field
, , relate between customer
, custom_field
, again becomes unmanageable when consider every table potentially have custom fields.
a single field value ever apply single entity table.
as aside want create attachments
table managing uploaded attachments particular entity, , again apply customers, suppliers, products , various other tables, it's similar issue.
Comments
Post a Comment