Skip to content

Instantly share code, notes, and snippets.

@msakrejda
Created May 19, 2015 02:51
Show Gist options
  • Save msakrejda/2fd9435fd0a453063e9e to your computer and use it in GitHub Desktop.
Save msakrejda/2fd9435fd0a453063e9e to your computer and use it in GitHub Desktop.
sequel association issue
[1] pry(main)> require 'sequel'
=> true
[2] pry(main)> DB = Sequel.connect('postgres:///maciek')
=> #<Sequel::Postgres::Database: "postgres:///maciek">
[3] pry(main)> DB.run <<-EOF
[3] pry(main)* CREATE TABLE a(id serial primary key);
[3] pry(main)* CREATE TABLE b(id serial primary key, a_id integer);
[3] pry(main)* EOF
=> nil
[4] pry(main)> class A < Sequel::Model(:a)
[4] pry(main)* one_to_one :b, key: :a_id, primary_key: :id
[4] pry(main)* end
=> ...
[5] pry(main)> class B < Sequel::Model(:b)
[5] pry(main)* many_to_one :a, key: :a_id, primary_key: :id, class: "A"
[5] pry(main)* end
=> ...
[6] pry(main)> b = B.create
=> #<B @values={:id=>1, :a_id=>nil}>
[7] pry(main)> A.create(b: b)
Sequel::Error: object #<A @values={}> does not have a primary key
from /home/maciek/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/sequel-4.17.0/lib/sequel/model/associations.rb:2352:in `set_one_to_one_associated_object'
[8] pry(main)> a = A.create
=> #<A @values={:id=>1}>
[9] pry(main)> B.create(a: a)
=> #<B @values={:id=>2, :a_id=>1}>
[10] pry(main)>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment