Skip to content

Instantly share code, notes, and snippets.

@srhyne
Last active August 6, 2018 22:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save srhyne/8e3207ca1ce3b92d94aed168dded9869 to your computer and use it in GitHub Desktop.
Save srhyne/8e3207ca1ce3b92d94aed168dded9869 to your computer and use it in GitHub Desktop.
Normalize polymorphic foreign relationships using union all
-- using union all to normalize
select id, name, foo, bar from (
select * from messages
left join broadcasts on broadcasts.id = messages.type_id
where type = 'broadcast'
union
select * from messages
left join auto_followups on auto_followups.id = messages.type_id
where type = 'auto-follow-up'
) t;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment