Hello, I'm new to Sequel, so please excuse my maybe simple question. 

I want to set up a many to many association. When adding such an 
association, it leads to a UNIQUE constraint violation. It seems like I 
miss something here. Maybe you could help me, please? I have the following 
code (simplified):

--------------------------------- tables ---------------------------------

db.create_table? :tickets do
      primary_key   :id
      String            :key
      String            :ticket_type_name
      String            :priority
end

db.create_table? :editors do
      Integer           :employee_number, primary_key: true
      String            :name
end

db.create_table?  :tickets_editors do
      foreign_key   :ticket_id, :tickets
      foreign_key   :editor_id, :editors
      primary_key  [:ticket_id, :editor_id]
end

--------------------------------- models ---------------------------------
class Ticket < Sequel::Model
      many_to_many :editors
end 

class Editor < Sequel::Model
      many_to_many :tickets
      unrestrict_primary_key
end 


# I'm retrieving data from an API, create for every ticket a *ticket *instance 
and also an *editor *variable which is a hash containing the values to add

# [each loop:]
*ticket*.add_editor(*editor*)

# Now it is the case that different tickets have the same editors and it 
seems like that editors are added multiple times to the editor table. As if 
it isn't checked if it's already there in the editor table. But the 
association in the join table is done correctly. 

--------------------------------- console output 
---------------------------------
/var/lib/gems/2.3.0/gems/sqlite3-1.4.0/lib/sqlite3/resultset.rb:108:in 
`step': SQLite3::ConstraintException: UNIQUE constraint failed: 
editors.employee_number (Sequel::UniqueConstraintViolation)
        from 
/var/lib/gems/2.3.0/gems/sqlite3-1.4.0/lib/sqlite3/resultset.rb:108:in 
`next'
        from 
/var/lib/gems/2.3.0/gems/sqlite3-1.4.0/lib/sqlite3/resultset.rb:133:in 
`each'
        from 
/var/lib/gems/2.3.0/gems/sqlite3-1.4.0/lib/sqlite3/database.rb:202:in `to_a'
        from 
/var/lib/gems/2.3.0/gems/sqlite3-1.4.0/lib/sqlite3/database.rb:202:in 
`block in execute'
        from 
/var/lib/gems/2.3.0/gems/sqlite3-1.4.0/lib/sqlite3/database.rb:151:in 
`prepare'
        from 
/var/lib/gems/2.3.0/gems/sqlite3-1.4.0/lib/sqlite3/database.rb:193:in 
`execute'
        from 
/var/lib/gems/2.3.0/gems/sequel-5.19.0/lib/sequel/adapters/sqlite.rb:202:in 
`block (2 levels) in _execute'
        from 
/var/lib/gems/2.3.0/gems/sequel-5.19.0/lib/sequel/database/logging.rb:38:in 
`log_connection_yield'
        from 
/var/lib/gems/2.3.0/gems/sequel-5.19.0/lib/sequel/adapters/sqlite.rb:202:in 
`block in _execute'
        from 
/var/lib/gems/2.3.0/gems/sequel-5.19.0/lib/sequel/database/connecting.rb:253:in 
`block in synchronize'
        from 
/var/lib/gems/2.3.0/gems/sequel-5.19.0/lib/sequel/connection_pool/threaded.rb:88:in
 
`hold'
        from 
/var/lib/gems/2.3.0/gems/sequel-5.19.0/lib/sequel/database/connecting.rb:253:in 
`synchronize'
        from 
/var/lib/gems/2.3.0/gems/sequel-5.19.0/lib/sequel/adapters/sqlite.rb:193:in 
`_execute'
        from 
/var/lib/gems/2.3.0/gems/sequel-5.19.0/lib/sequel/adapters/sqlite.rb:159:in 
`execute_insert'
        from 
/var/lib/gems/2.3.0/gems/sequel-5.19.0/lib/sequel/dataset/actions.rb:1104:in 
`execute_insert'
        from 
/var/lib/gems/2.3.0/gems/sequel-5.19.0/lib/sequel/dataset/actions.rb:400:in 
`insert'
        from 
/var/lib/gems/2.3.0/gems/sequel-5.19.0/lib/sequel/model/base.rb:1704:in 
`_insert_raw'
        from 
/var/lib/gems/2.3.0/gems/sequel-5.19.0/lib/sequel/model/base.rb:1686:in 
`_insert'
        from 
/var/lib/gems/2.3.0/gems/sequel-5.19.0/lib/sequel/model/base.rb:1753:in 
`block (2 levels) in _save'
        from 
/var/lib/gems/2.3.0/gems/sequel-5.19.0/lib/sequel/model/base.rb:1015:in 
`around_create'
        from 
/var/lib/gems/2.3.0/gems/sequel-5.19.0/lib/sequel/model/base.rb:1750:in 
`block in _save'
        from 
/var/lib/gems/2.3.0/gems/sequel-5.19.0/lib/sequel/model/base.rb:1015:in 
`around_save'
        from 
/var/lib/gems/2.3.0/gems/sequel-5.19.0/lib/sequel/model/base.rb:1745:in 
`_save'
        from 
/var/lib/gems/2.3.0/gems/sequel-5.19.0/lib/sequel/model/base.rb:1465:in 
`block (2 levels) in save'
        from 
/var/lib/gems/2.3.0/gems/sequel-5.19.0/lib/sequel/model/base.rb:1905:in 
`block in checked_transaction'
        from 
/var/lib/gems/2.3.0/gems/sequel-5.19.0/lib/sequel/database/transactions.rb:245:in
 
`_transaction'
        from 
/var/lib/gems/2.3.0/gems/sequel-5.19.0/lib/sequel/database/transactions.rb:220:in
 
`block in transaction'
        from 
/var/lib/gems/2.3.0/gems/sequel-5.19.0/lib/sequel/database/connecting.rb:253:in 
`block in synchronize'
        from 
/var/lib/gems/2.3.0/gems/sequel-5.19.0/lib/sequel/connection_pool/threaded.rb:92:in
 
`hold'
        from 
/var/lib/gems/2.3.0/gems/sequel-5.19.0/lib/sequel/database/connecting.rb:253:in 
`synchronize'
        from 
/var/lib/gems/2.3.0/gems/sequel-5.19.0/lib/sequel/database/transactions.rb:186:in
 
`transaction'
        from 
/var/lib/gems/2.3.0/gems/sequel-5.19.0/lib/sequel/model/base.rb:1905:in 
`checked_transaction'
        from 
/var/lib/gems/2.3.0/gems/sequel-5.19.0/lib/sequel/model/base.rb:1465:in 
`block in save'
        from 
/var/lib/gems/2.3.0/gems/sequel-5.19.0/lib/sequel/model/base.rb:1893:in 
`checked_save_failure'
        from 
/var/lib/gems/2.3.0/gems/sequel-5.19.0/lib/sequel/model/base.rb:1465:in 
`save'
        from 
/var/lib/gems/2.3.0/gems/sequel-5.19.0/lib/sequel/model/associations.rb:2444:in 
`ensure_associated_primary_key'
        from 
/var/lib/gems/2.3.0/gems/sequel-5.19.0/lib/sequel/model/associations.rb:2374:in 
`add_associated_object'
        from 
/var/lib/gems/2.3.0/gems/sequel-5.19.0/lib/sequel/model/associations.rb:1959:in 
`block in def_association_instance_methods'
        from /home/[user]/[project]/app/models/editor.rb:50:in `block in 
add_editors'
        from /home/[user]/[project]/app/models/editor.rb:42:in `each'
        from /home/[user]/[project]/app/models/editor.rb:42:in `add_editors'
        from /home/[user]/[project]/app/models/ticket.rb:49:in `add'
        from /home/[user]/[project]/importer.rb:54:in `block in import_data'
        from /home/[user]/[project]/importer.rb:32:in `each'
        from /home/[user]/[project]/importer.rb:32:in `import_data'

What do I have to do that first the editor table is checked whether the 
editor already exists and if, just takes the primary key of the existing 
one for the association in the join table instead of creating a new one? 
What did I miss here?
Thank you in advance for help and explanation!

- Jay

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sequel-talk.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sequel-talk/cba134f1-0f19-4ec1-8b23-d403dc7b45ff%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to