DBIx :: Class查询失败,但从DBIC_TRACE转储工作

时间:2012-10-09 18:41:08

标签: perl orm dbix-class

我现在无法重写当前的数据库架构BTW,但这与我遇到的问题无关,所以请忽略表结构:D

我正在运行此数据库查询:

my $rs = $dbx->resultset('Result')->search(
           {
             'result_hnd'         => 16078055,
             'seasons.outdoor'    => 'venue.outdoors',
             'seasons.start_date' => { '<=' => 'meet.date_end'   },
             'seasons.end_date'   => { '>=' => 'meet.date_begin' },
           },
           {
             'join' => [
               {
                 'team' => {
                   'league_teams' => {
                     'league' => 'seasons',
                   },
                 },
               },
               {
                 'meet' => 'venue'
               },
             ],
             '+select' => ['seasons.season_hnd','seasons.name','seasons.start_date','seasons.end_date','meet.date_begin','meet.date_end'],
             '+as'     => ['season_hnd','season_name','s_start','s_end','m_start','m_end'],
             columns   => ['result_hnd'],
             group_by  => ['seasons.season_hnd'],
           }
         );

当我运行时,我没有得到任何结果。启用DBIC_TRACE后,我将生成的SQL视为:

SELECT me.result_hnd, seasons.season_hnd, seasons.name, seasons.start_date, seasons.end_date, meet.date_begin, meet.date_end FROM track.result me JOIN track.team team ON team.team_hnd = me.team_hnd LEFT JOIN track.league_team league_teams ON league_teams.team_hnd = team.team_hnd LEFT JOIN track.league league ON league.league_hnd = league_teams.league_hnd LEFT JOIN track.season seasons ON seasons.league_hnd = league.league_hnd OR seasons.league_hnd = league.parent_league_hnd JOIN track.meet meet ON meet.meet_hnd = me.meet_hnd JOIN track.venue venue ON venue.venue_hnd = meet.venue_hnd WHERE ( ( result_hnd = ? AND seasons.end_date >= ? AND seasons.outdoor = ? AND seasons.start_date <= ? ) ) GROUP BY seasons.season_hnd: '16078055', 'meet.date_begin', 'venue.outdoors', 'meet.date_end'

当我将此语句复制并粘贴到我的MYSQL客户端(并插入占位符)时,如下所示:

SELECT me.result_hnd, seasons.season_hnd, seasons.name, seasons.start_date, seasons.end_date, meet.date_begin, meet.date_end 
  FROM track.result me 
      JOIN track.team team ON team.team_hnd = me.team_hnd 
      LEFT JOIN track.league_team league_teams ON league_teams.team_hnd = team.team_hnd 
      LEFT JOIN track.league league ON league.league_hnd = league_teams.league_hnd 
      LEFT JOIN track.season seasons ON seasons.league_hnd = league.league_hnd OR seasons.league_hnd = league.parent_league_hnd 
      JOIN track.meet meet ON meet.meet_hnd = me.meet_hnd 
      JOIN track.venue venue ON venue.venue_hnd = meet.venue_hnd 
WHERE ( ( result_hnd = 16078055 AND seasons.end_date >= meet.date_begin AND seasons.outdoor = venue.outdoors AND seasons.start_date <= meet.date_end ) ) 
  GROUP BY season_hnd;

我得到了我期望的确切结果(7条记录)。

这真是奇怪。对于所有意图和目的,是不是完全相同的查询?我在调试中遗漏了什么?或者是在DBIx :: Class :: ResultSet层没有被转储的其他事情?

2 个答案:

答案 0 :(得分:4)

告诉SQL :: Abstract右边的值实际上是一个标识符,你可以执行以下操作(如docs中所述):

   {
     'result_hnd'         => 16078055,
     'seasons.outdoor'    => { -ident => 'venue.outdoors' },
     'seasons.start_date' => { '<=' => { -ident => 'meet.date_end' }   },
     'seasons.end_date'   => { '>=' => { -ident => 'meet.date_begin' } },
   },

答案 1 :(得分:1)

  

对于所有意图和目的,是不是完全相同的查询?

For 所有意图和目的,......)

没有。在日志中,您有相当于

的内容
result_hnd = '16078055'
AND seasons.end_date >= 'meet.date_begin'
AND seasons.outdoor = 'venue.outdoors'
AND seasons.start_date <= 'meet.date_end'

或者

result_hnd = 16078055
AND seasons.end_date >= 'meet.date_begin'
AND seasons.outdoor = 'venue.outdoors'
AND seasons.start_date <= 'meet.date_end'

在您的尝试中,您使用了

result_hnd = 16078055
AND seasons.end_date >= meet.date_begin
AND seasons.outdoor = venue.outdoors
AND seasons.start_date <= meet.date_end

抱歉,我没有适合您的解决方案。