Jeremy Evans wrote:
> On May 28, 10:38 pm, Joel VanderWerf <[email protected]> wrote:
>> (1) why does
>>
>>      entries = items.where("id" => id)
>>
>> fail, when "id" is apparently an acceptable substitute for :id in #<< ?
> 
> Because "id" in the filter is an SQL string.  If you want an SQL
> identifier, you should use :id.

So the contents of that hash key could potentially be some other SQL 
string besides a column name? Or is this just for column names with 
spaces, symbols, etc.?

>  The only reason "id" works in insert/
> << is that you can't use an SQL string in its place, so it assumes you
> mean an identifer:
> 
>   UPDATE items SET 'id' = 3; -- Invalid SQL
> 
>   items.where(:id => id)  # WHERE id = 3
>   items.where('id' => id) # WHERE 'id' = 3
> 
> In general you should always use symbols for SQL identifiers (columns/
> tables) when you are using Sequel.

Makes sense. I tend to prefer strings as keys in YAML (and by necessity 
JSON) documents, so I'll just get used to always interning, when the key 
is not a literal.

>> (2) Is there a way to express the if... as a single operation, with the
>> same semantics (i.e. not failing when there's already an entry)?
> 
> If you are using MySQL, you can use:
> 
>   ds[:items].replace(:id=>id)
> 
> That'll use a single REPLACE query.  REPLACE isn't standard SQL
> though, and I don't think any other adapters support it.  Models have
> a find_or_create method which does something similar.  Your way isn't
> a bad way to do things.

Ok, I don't feel so clumsy now. Thanks!

-- 
       vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
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