English 中文(简体)
ActiveRecord+SQLite 3的行为异常
原标题:ActiveRecord + SQLite 3 behaves strangely

我使用活动记录进行了此查询

AdImage.select("ad_images.id, ad_images.locale_id, ad_campaigns.click_url,
ad_campaigns.default_ad_image_id").joins("left outer join ad_campaigns on
ad_campaigns.id = ad_images.ad_campaign_id").where("ad_images.ad_campaign_id" => 1)

它生成以下sql查询:

SELECT ad_images.id, ad_images.locale_id, ad_campaigns.click_url,
ad_campaigns.default_ad_image_id FROM "ad_images" left outer join ad_campaigns on
ad_campaigns.id = ad_images.ad_campaign_id WHERE "ad_images"."ad_campaign_id" = 1

结果如下:

 => [#<AdImage id: 22, click_url: "market://details?id=com.mobiata.flighttrack",
    locale_id: 2>] 

这是错误的。

所以我使用ActiveRecord::Base.connection.execute方法直接运行sql查询:

ActiveRecord::Base.connection.execute("SELECT ad_campaigns.click_url, ad_images.id, 
ad_images.locale_id, ad_campaigns.default_ad_image_id FROM ad_campaigns inner join
ad_images on ad_campaigns.id = ad_images.ad_campaign_id WHERE ad_images.ad_campaign_id = 1")

其返回以下内容:

[{"click_url"=>"market://details?id=com.mobiata.flighttrack", "id"=>22, "locale_id"=>2,
"default_ad_image_id"=>22, 0=>"market://details?id=com.mobiata.flighttrack", 1=>22,
2=>2, 3=>22}] 

其中有奇怪的重复。

第一个和第二个之间的唯一区别是表名中的“ad_images”与ad_image。

我的问题是:

1) 我不明白是什么造成了这种不同。

2) 为什么第二个查询在SQLite3中返回垃圾,而在MySQL服务器中没有返回垃圾?

最佳回答
  1. 我最终使用了“ActiveRecord::Base.connection.execute”,而不是使用Rails ActiveRecord助手。似乎没有其他解决方案。

  2. 事实证明,在调用值时,应该使用索引值,而不是双引号列名。否则,在MySQL的生产中使用时,您将遇到类型错误。

问题回答

暂无回答




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签