P
Phil
I'm trying to construct a DB that's essentially a list of lists, with
each sublist having an identical structure. Seems straightforward,
right? Make a single table for ALL the items, to keep the list
structure consistent; include a field to say which list each item
belongs to; and make that part of a composite PK because the item
numbers are only unique across the list they belong to. And, for
completion's sake, create a table of lists (which I might use for things
like the list titles), and make the aforementioned field FK to it.
The problem is, an item can deserve a place in more than one list, and
updates to an item should be reflected to every list it appears in
simultaneously. It's not MtM, because each place in a given list can
only be occupied by one item, but I created a separate "places" table
anyway, since it'd be wasteful to create fields in the items table that
would invalidate the rest of the record if filled.
The places table contains four fields: a list/item pair for a given
place on a list, and a list/item pair specifying its "real" location in
the items table (that is, where it was originally added; for the first
(or only) reference to an item, these two pairs of fields would be
identical).
Next, I added more relations to enforce referential integrity: A
place's list must exist in the lists table; an item must be used at
least once as a "real" location in the places table (or it has no reason
to exist); and an item must also be used EXACTLY once as an "apparent"
location in the places table. (If an item is used in one or more other
places and then removed from its original place, I have to write code to
renumber the item to match one of the remaining places anyway, because
its old place may need to be reused for some other item.)
Unfortunately, this works too well - now I can't add any items because
"a related record is required in table 'places'", but I can't add any
places because "a related record is required in table 'items'"! What
can I do to fix this, without losing the necessary constraints?
each sublist having an identical structure. Seems straightforward,
right? Make a single table for ALL the items, to keep the list
structure consistent; include a field to say which list each item
belongs to; and make that part of a composite PK because the item
numbers are only unique across the list they belong to. And, for
completion's sake, create a table of lists (which I might use for things
like the list titles), and make the aforementioned field FK to it.
The problem is, an item can deserve a place in more than one list, and
updates to an item should be reflected to every list it appears in
simultaneously. It's not MtM, because each place in a given list can
only be occupied by one item, but I created a separate "places" table
anyway, since it'd be wasteful to create fields in the items table that
would invalidate the rest of the record if filled.
The places table contains four fields: a list/item pair for a given
place on a list, and a list/item pair specifying its "real" location in
the items table (that is, where it was originally added; for the first
(or only) reference to an item, these two pairs of fields would be
identical).
Next, I added more relations to enforce referential integrity: A
place's list must exist in the lists table; an item must be used at
least once as a "real" location in the places table (or it has no reason
to exist); and an item must also be used EXACTLY once as an "apparent"
location in the places table. (If an item is used in one or more other
places and then removed from its original place, I have to write code to
renumber the item to match one of the remaining places anyway, because
its old place may need to be reused for some other item.)
Unfortunately, this works too well - now I can't add any items because
"a related record is required in table 'places'", but I can't add any
places because "a related record is required in table 'items'"! What
can I do to fix this, without losing the necessary constraints?