I have the following named_scope which works fine in MySQL and sqlite but bombs in Postgres:
course.rb
named_scope :current, :conditions => ['start < ? AND end > ? ', Time.now, Time.now], :order => 'start ASC'
Then I just call:
Course.current
I get the error:
PGError: ERROR: syntax error at or
near "end" LINE 1: ... WHERE (start <
'2010-03-17 14:03:24.995746' AND end >
'201...
^ : SELECT count(*) AS count_all FROM
"courses" WHERE (start <
'2010-03-17 14:03:24.995746' AND end >
'2010-03-17 14:03:24.995748' )
My google-fu is failing me so I'm hoping Stack Overflow won't. Any ideas on how to make that scope Postgres-friendly? Thanks,
Drew
END is a keyword, you have to use another name or place it between double quotes "end".
If you use double quotes around the columnname and use this code also for MySQL, tell MySQL to accept double quotes as object identifier by setting the correct SQL MODE: ANSI_QUOTES
Related
I am trying to prepare subnet=adress/netmask for presentation, and start_ip and end_ip for filtering purpose under lifs qs. then I want to use subquery to cut of single interface as vserver may have multiple. Problem is that I can't pass extra field generated in lifs queryset.
lifs = (NetworkLif.objects.filter(vserverid=OuterRef('pk'))
.annotate(network=Concat('address', Value('/'), 'netmasklength', output_field=CharField()))
.extra(select={'start_ip': "INET_NTOA(INET_ATON(address) & 0xffffffff ^ ((0x1 << ( 32 - netmasklength) ) -1 ))"})
.extra(select={'end_ip': "INET_NTOA(INET_ATON(address) | ((0x100000000 >> netmasklength ) -1 ))"})
.order_by('network')
)
queryset = (Vserver.objects.filter(type='DATA')
.exclude(name__endswith='-mc')
.annotate(subnet=Subquery(lifs.values('network' )[:1]),
#.annotate(start_ip_address=Subquery(lifs.values('start_ip')[:1]))
#.annotate(end_ip_address=Subquery(lifs.values('end_ip')[:1]))
.values('id', 'clusterid__name', 'name', 'state', 'nfsenabled', 'cifsenabled', 'ldapclientenabled', 'dnsenabled', 'subnet', )#'start_ip_address')#, 'end_ip_address' )
.order_by('id')
)
getting this error, I guess this is becuase evaulation logic
raise FieldError("Cannot resolve expression type, unknown output_field")
django.core.exceptions.FieldError: Cannot resolve expression type, unknown output_field
is there any workaround? I know in postgresql distinct('name') would fix my problem. unfortunetelly ActiveIQ is running on mysql.
when I use \Doctrine\Common\Collections\Criteria::create()
use Doctrine\Common\Collections\Criteria;
...
$criteria = Criteria::create();
$criteria->where(Criteria::expr()->eq('isPublished', 1))
->andWhere(Criteria::expr()->eq('isDeleted', 0));
$this->comments->matching($criteria)
and I getting error:
Message:
An exception occurred while executing 'SELECT t0.id AS id1, t0.rating AS rating2, t0.text AS text3, t0.username AS username4, t0.isPublished AS isPublished5, t0.isDeleted AS isDeleted6, t0.dateCreated AS dateCreated7, t0.userIP AS userIP8, t0.user_id AS user_id9, t0.product_id AS product_id10 FROM product_comments t0 WHERE ((t0.isPublished IS ? AND t0.isDeleted IS ?) AND t0.product_id IS ?)' with params {"1":1,"2":0,"3":1123}:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1 AND t0.isDeleted IS 0) AND t0.product_id*IS 1123)' at line 1
The problem is operand 'IS' in where clausule. It is not MySQL operand. (If I paste this query to MySQL terminal, and change "IS" => "=" - is all right)
Why Doctrine genetate such query? Where is the problem?
I solved changing line 91 of Doctrine\ORM\Persisters\BasicEntityPersister
from
Comparison::IS => 'IS %s',
to
Comparison::IS => '= %s',
This is a bug in doctrine fixed by upgrading Doctrine ORM to 2.3.5 or later.
Bug report at http://www.doctrine-project.org/jira/browse/DDC-2471
More discussion on the problem at
https://github.com/doctrine/collections/commit/3db3ab843ff76774bee4679d4cb3a10cffb0a935#diff-757942c669bf6be9910786b2558ad745
Try replacing
expr()->eq('isPublished', 1) and expr()->eq('isPublished', 0) with
expr()->eq('isPublished', '?1')
expr()->eq('isPublished', '?0')
I have the following query that selects some records from the database:
$select_person = $this->select()
->setIntegrityCheck(false)
->from(array('a' => 'tableA'),
array(new Zend_Db_Expr('SQL_CALC_FOUND_ROWS a.id'),
'a.cid',
'a.email',
'final' => new Zend_Db_Expr( "concat( '<div
style=\"color:#1569C7; font-weight:bold\">',
a.head , ' ' , a.tail, '</div>')" ),
'a.red_flag'
)
)
->joinLeft(array('b' => 'tableb'), ... blah blah)
->where('blah blah')
->order('a.head ASC')
I want to modify the above query so that it selects a different value for 'final' depending on the value of
a.red_flag.
which can have values - true or false.
I understand I can use the CASE statement of mysql - eg something like the following:
'final' => new Zend_Db_Expr("CASE a.red_flag WHEN 'true' THEN '$concatstr1'
ELSE '$concatstr2' END")
The value of $concatstr1 = "concat( '<div style=\"color:red; font-weight:bold\">', a.head , ' ' , a.tail, '</div>')" ;
The value of $concatstr2 = "concat( '<div style=\"color:blue; font-weight:bold\">', a.head , ' ' , a.tail, '</div>')" ;
However, it throws an error saying
Message: SQLSTATE[42000]: Syntax error or access violation: 1064
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'div
style="color:red; font-weight:bold">',
a.head , ' ' , ' at line 1
How can I make this query work?
Any help is greatly appreciated.
Thanks
Personnaly, I don't like to get HTML as an answer from the DB. It gets confusing and harder to debug and change afterwards. Furthermore, you might get some errors due to the confusion with the ' and " and all the reserved characters in MySQL (<, >, ;, ...) I would suggest that you try this:
'final' => new Zend_Db_Expr("CASE a.red_flag WHEN 'true' THEN 1
ELSE 0 END")
Then do a check on the value of a.red_flag;
if($this->final) {
$output .= '<div style=\"color:red; font-weight:bold\">';
} else {
$output .= '<div style=\"color:blue; font-weight:bold\">';
}
$output .= $this->head.' '.$this->tail;
$output .= '</div>';
If the query still doesn't work. Try
echo $select->__toString; exit();
and check the query. Try the output that you got with the __toString on your database and check if it works. It's easier to fix it that way. You could also show the query string here and it'll be easier to debug.
Finally, I found the error in my statement.
The culprit was - I was using quotes in $concatstr1 and $concatstr2 inside the $select_person statement.
The correct query should be formed as follows:
$select_person = $this->select()
->setIntegrityCheck(false)
->from(array('a' => 'tableA'),
array(new Zend_Db_Expr('SQL_CALC_FOUND_ROWS a.id'),
'a.cid',
'a.email',
final' => new Zend_Db_Expr("CASE a.red_flag WHEN 'true' THEN $concatstr1 ELSE $concatstr2 END"),
'a.red_flag'
)
)
->joinLeft(array('b' => 'tableb'), ... blah blah)
->where('blah blah')
->order('a.head ASC');
This is now returning me the appropriate value of 'final' - concatstr1 when red_flag is true otherwise it is returning me concatstr2.
while running the below mysql query in ruby on rails
cnt=Domainurl.find_by_sql["SELECT MAX(`count`) FROM domainurls WHERE `domaindetail_id` = ?", #domain.id]
puts cnt.count
I am getting below error:
"error is: wrong number of arguments (0 for 1)"
Can anybody tell where exactly am I wrong?
You either need to wrap the argument in parenthesis or add a space.
cnt=Domainurl.find_by_sql(["SELECT MAX(`count`) FROM domainurls WHERE `domaindetail_id` = ?", #domain.id])
or
cnt=Domainurl.find_by_sql ["SELECT MAX(`count`) FROM domainurls WHERE `domaindetail_id` = ?", #domain.id]
Model:
class TechRequest < ActiveRecord::Base
...
define_index do
...
indexes :hot_request
indexes :status_id, :as => :current_status_id
...
has :hot_request , :as => :hot_request
set_property :delta => true
end
DB:
hot_request - tinyint(1)
When I execute the controller code-
#query_string = '(#hot_request 1)(#current_status_id 1 | 2 | 3)'
#tech_requests = TechRequest.search #query_string, :match_mode => :extended
the following error is thrown up:
ThinkingSphinx::SphinxError: index tech_request_core,tech_request_delta: query error: no field 'tech_hot_request' found in schema
from D:/Current/TechAssistTest/vendor/plugins/thinking-sphinx/lib/thinking_sphinx/search.rb:392:in 'populate'
from D:/Current/TechAssistTest/vendor/plugins/thinking-sphinx/lib/thinking_sphinx/search.rb:508:in 'call'
from D:/Current/TechAssistTest/vendor/plugins/thinking-sphinx/lib/thinking_sphinx/search.rb:508:in 'retry_on_stale_index'
from D:/Current/TechAssistTest/vendor/plugins/thinking-sphinx/lib/thinking_sphinx/search.rb:379:in 'populate'
from D:/Current/TechAssistTest/vendor/plugins/thinking-sphinx/lib/thinking_sphinx/search.rb:167:in 'method_missing'
from D:/ruby/lib/ruby/1.8/irb.rb:302:in 'output_value'
from D:/ruby/lib/ruby/1.8/irb.rb:151:in 'eval_input'
from D:/ruby/lib/ruby/1.8/irb.rb:263:in 'signal_status'
from D:/ruby/lib/ruby/1.8/irb.rb:147:in 'eval_input'
from D:/ruby/lib/ruby/1.8/irb.rb:146:in 'eval_input'
from D:/ruby/lib/ruby/1.8/irb.rb:70:in 'start'
from D:/ruby/lib/ruby/1.8/irb.rb:69:in 'catch'
from D:/ruby/lib/ruby/1.8/irb.rb:69:in 'start'
from D:/ruby/bin/irb:13
The search works fine when I use hot_request as an attribute. The
search also works fine when I use #query_string = '(#current_status_id 1 | 2 | 3)'.
I've just run into similar looking problems - there are two possible reasons why this errors that I can see. First is that according to http://sphinxsearch.com/forum/view.html?id=2103 you can use an sql column as a field or an attribute but not both (without cloning it). The other, which had me baffled for a while, is that you may need to specify the type - so if hot_request is actually an integer, you probably need to have something like
indexes hot_request :as => hr, :type => :integer
or you get that cryptic error message
Hope this helps someone ...