Many to many relationship to itself in PostgreSQL -
using postgresql, have table should have many many relationship itself. table called tools , i'm doing many many secondary table, called integrations, has 2 fields, tool_a_id , tool_b_id.
is there better way? problem whenever want find integrations need check both tool_a , tool_b, in:
select * integrations tool_a_id = x , tool_b_id = x or when want select tools integrate one, have do:
select "tools".* tools, integrations ((tools.id = integrations.tool_a_id , integrations.tool_b_id = x) or (tools.id = integrations.tool_b_id , integrations.tool_a_id = x)) this example, doesn't allow define integrations relationship in rails rails expects 1 , 1 foreign key match. there better way? feels inelegant. don't mind being stuck postgresql.
well, that's way we've learned rdbms books. ;)
but in sql example, you're not defining objects correctly. think might looking more (with same database schema):
select t1.* tools t1 join integrations on (t1.id = i.tool_a_id) join tools t2 on (t2.id = i.tool_b_id) t2.id = x that more elegant, labels t1 list of tools pick , t2 list select 1 tool want know ones integrated it.
Comments
Post a Comment