I’m considering how I want to implement my “addresses” table in a new solution, and am debating a little bit different strategy. I thought I’d post it for comment, and to see how others are handling similar situations.
So, problem is, I want to factor addresses into their own table. Something like:
Addresses
address_id
address_type
address_name
address_street
address_city
etc.
I’ve got multiple different tables that will have n-m relations to addresses. Companies, Projects, etc. Ok, nothing new here, I know.
I do not want to create intermediary join tables for all these relations. Yuck. I’d rather do a polymorphic relation on the address table, where I reference the ID and reference type for the other side of the relation. So I would add these fields to the address table:
reference_id
reference_type
Then my relation from Companies to Addresses would be:
agiletortoise:
2) define a stored calculation on my tables “table_id” that redundantly stores the name of the table in every record, and use that field in the relation.
You don’t have to have a stored calculation for this. As long as you use it on the left side of the relation you can use an unstored calculation (just like you can use a global for this).
Isn’t the number of connections to the address table somehow limited? You have projects, people, … that have addresses. Couldn’t you create a few foreign keys in addresses like project_id? I have been using both of your techniques and they worked fine. I also the approach with a few foreign keys in certain situations. Just go for one. They all work.
You don’t have to have a stored calculation for this. As long as you use it on the left side of the relation you can use an unstored calculation (just like you can use a global for this).
Oh yes! I have overseen that part. Some of my tables have an (unstored) calc that says
I am using a blend of Patrick and Robert’s methods with a global in the relation (set from a table of tables within the calling method … works fine … as do multiple fk’s if you prefer a more traditional pk-fk model for your db model.
Harjo is pointing out that Greg will lose the opportunity to reuse the same address record more than once in not using join tables.
I am sure that Greg is aware of this in his choice to disregard using join tables and so to Greg I say that if you are happy that this schema supports what you need to represent then my preference would be to do it by replicating separate foreign keys in the address table as follows:
Ok, I’m dense. Don’t know why I didn’t realize you could use an unstored calc in a relationship…that makes my life easier.
Yes, I know I can’t re-use the same address in this scenario – but, frankly, I’ve never found addresses turn out to be that reusable anyway. Users always want to add that special marking for that one order, or such – and want that permanent record of what was attached to an order/invoice, etc.
On the multiple fields for the keys, I personally prefer the cleanliness I of my approach in this case, because I’d never have multiple relations to the same address. I do use that approach for other things I cross link, like notes/to-dos/activities.