On Thursday, November 19, 2015 at 7:47:40 AM UTC-8, Fransez wrote:
>
> Hi everybody,
>
> I have a question about Database '[]' method. The documentation says, it 
> has two behaviours: DB['sql statement'] is an alias for fetch, when 
> DB[:table_name] is an alias for #from. Both of them shall return a Dataset. 
> Ok. So, I tried the following:
> set = db[:my_table].where(:id => 2).all
>
> It works as excepted: I get the row(s) with id = 2 of my_table.
>
> Now, I try the other syntax:
> set = db['my_table'].where(:id => 2).all
>
> I get all rows of my_table. The result is as I have written:
> set = db['select * from my_table'].all
>
> I though, as the '[]' method returns a Dataset in all cases, I could still 
> filter it. But in fact, the behaviour seems to be different.
>
> Is this normal? Did I missed something?
>

Yes, this is normal.  If you are providing custom SQL, it overrides other 
options in the dataset.  Sequel doesn't parse SQL, so it wouldn't know 
where to add the WHERE clause.  If you want to do this, you can use 
from_self to move the query to a subselect:

db['select * from my_table'].from_self.where(:id=>1)
# SELECT * FROM (select * from my_table) AS [T1] WHERE ([ID] = 1)

Thanks,
Jeremy

-- 
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 http://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to