Hi all, 

I'm new to Sequel and am trying to solve the case in a many-to-many 
relationship where the join table has a third column that is used to define 
relationship 'type'.  A common example might be to define the role of an 
account related to some "thing".  

Here's an example to get started:

class Account < Sequel::Model

set schema do

primary_key :id

end

 

one_to_many :accounts_posts 

many_to_many :posts, :through => :accounts_posts

end


class Post < Sequel::Model

set schema do

primary_key :id

end 

 

one_to_many    :accounts_posts 

many_to_many :accounts, :through => :accounts_posts

end 

 

class AccountsPosts < Sequel::Model

 *# Fields:  post_id, account_id*, *role_id*

 set schema do

foreign_key :account_id, :accounts

foreign_key :post_id, :posts

integer :role_id, :null => false, :default => 1    # assume 1 is 'Owner' 
role, doesn't really matter though

primary_key [:account_id, :post_id, :role_id] 

end  

 

one_to_many :accounts

one_to_many :posts 

end 

 
I realize defining the AccountsThings class is unnecessary when it's a 
simple many_to_many.  However, because multiple Account objects can be 
associated with a single Post, each with one or more roles (e.g. Owner, 
Observer, Editor, etc), I get "role_id is a restricted primary key" error 
messages if I try to do the following:

ats = AccountsPosts.new(:role_id => Role::OWNER)

ats.account = @account

ats.post      = @post

ats.save  #=> error here


Wondering if anyone has solved this type of relationship before and could 
offer some pointers?  I'm hopeful I don't even need the AccountsPosts class 
and can use the Sequel DSL to achieve my goal in a way that I'm not 
familiar.  

Thanks,

--Chad 

 

 

 


 
 

 

 

 

 

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/sequel-talk/-/FQ_tcJTL4DIJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sequel-talk?hl=en.

Reply via email to