Skip to content
Browse files

Support composite keys with set_primary_key when called with an array…

… of multiple symbols

Before, this was only partially supported.  It broke the Model.[]
optimization if called with an array of multiple symbols.

The specs allowed the set_primary_key to work with both multiple
symbol arguments as well as single array of symbols argument, so
this just fixes the optimization for that case.
  • Loading branch information...
1 parent aa9f921 commit 7aa9bd20f52757420521d82b1ffa69265587dfd3 @jeremyevans jeremyevans committed
Showing with 14 additions and 2 deletions.
  1. +2 −0 CHANGELOG
  2. +2 −1 lib/sequel/model/base.rb
  3. +2 −0 spec/model/base_spec.rb
  4. +8 −1 spec/model/model_spec.rb
View
2 CHANGELOG
@@ -1,5 +1,7 @@
=== HEAD
+* Support composite keys with set_primary_key when called with an array of multiple symbols (jeremyevans)
+
* Fix select_more and order_more to not affect receiver (tamas.denes, jeremyevans)
* Support composite keys in model associations (jeremyevans)
View
3 lib/sequel/model/base.rb
@@ -317,8 +317,9 @@ def set_dataset(ds, opts={})
# You can set it to nil to not have a primary key, but that
# cause certain things not to work, see no_primary_key.
def set_primary_key(*key)
+ key = key.flatten
@simple_pk = key.length == 1 ? db.literal(key.first) : nil
- @primary_key = (key.length == 1) ? key[0] : key.flatten
+ @primary_key = (key.length == 1) ? key[0] : key
end
# Set the columns to restrict in new/set/update. Using this means that
View
2 spec/model/base_spec.rb
@@ -350,6 +350,8 @@ def simple_table
@c.simple_pk.should == nil
@c.set_primary_key :b, :a
@c.simple_pk.should == nil
+ @c.set_primary_key [:b, :a]
+ @c.simple_pk.should == nil
end
it "should have simple table set if passed a Symbol to set_dataset" do
View
9 spec/model/model_spec.rb
@@ -436,12 +436,19 @@ def fetch_rows(sql)
$sqls.last.should == "SELECT * FROM items WHERE (name = 'sharon') LIMIT 1"
end
- it "should work correctly for composite primary key" do
+ it "should work correctly for composite primary key specified as array" do
@c.set_primary_key [:node_id, :kind]
@c[3921, 201].should be_a_kind_of(@c)
$sqls.last.should =~ \
/^SELECT \* FROM items WHERE \((\(node_id = 3921\) AND \(kind = 201\))|(\(kind = 201\) AND \(node_id = 3921\))\) LIMIT 1$/
end
+
+ it "should work correctly for composite primary key specified as separate arguments" do
+ @c.set_primary_key :node_id, :kind
+ @c[3921, 201].should be_a_kind_of(@c)
+ $sqls.last.should =~ \
+ /^SELECT \* FROM items WHERE \((\(node_id = 3921\) AND \(kind = 201\))|(\(kind = 201\) AND \(node_id = 3921\))\) LIMIT 1$/
+ end
end
context "Model#inspect" do

0 comments on commit 7aa9bd2

Please sign in to comment.
Something went wrong with that request. Please try again.