DBIC 虚拟视图缺失数据

DBIC virtual view missing data

我在使用 DBIC 视图填充结果集时遇到问题。与联接相关的所有列 table 都是空的。

我的视图定义如下:

    package MySchema::CustomResults::ORDERS;
    
    use Modern::Perl;
    use base qw/DBIx::Class::Core/;
     
    __PACKAGE__->table_class('DBIx::Class::ResultSource::View');
     
    __PACKAGE__->table('ORDERS');
    __PACKAGE__->result_source_instance->is_virtual(1);
    
    __PACKAGE__->result_source_instance->view_definition(
        'SELECT p.PROJECT_ID, p.PROJECT, p.Center_Id, p.IMPORT_TIMESTAMP, p.Delivery_CountryCode, p.SHIP_DATE, pp.VALUE AS ORDER_TYPE, pp.ADD_TIMESTAMP
        FROM PROJECT p 
        LEFT JOIN PROJECT_ADDITIONS pp ON p.PROJECT_ID = pp.PROJECT_ID AND pp.FIELD = "ORDER_TYPE"
        WHERE p.PROJECT_ID IN (?)'
    );
    
    __PACKAGE__->add_columns(
        "project_id",
        { data_type => "integer", is_nullable => 0 },
        "project",
        { data_type => "varchar", is_nullable => 0, size => 50 },
        "center_id",
        { data_type => "varchar", is_nullable => 1, size => 50 },
        "delivery_countrycode",
        { data_type => "char", is_nullable => 1, size => 2 },
        "import_timestamp",
        {
            data_type => "datetime",
            datetime_undef_if_invalid => 1,
            default_value => "0000-00-00 00:00:00",
            is_nullable => 1,
        },
        "ship_date",
        {
            data_type => "datetime",
            datetime_undef_if_invalid => 1,
            is_nullable => 0,
        },
        "order_type",
        { data_type => "varchar", is_nullable => 1, size => 255 },
        "add_timestamp" =>
      {
        data_type => "datetime",
        datetime_undef_if_invalid => 1,
        default_value => "0000-00-00 00:00:00",
        is_nullable => 1,
      },
    );
    
    
    1;

如果我使用以下代码搜索数据集:

    my @associatedOrders = $self->schema->resultset('ORDERS')->search({},
        {
            bind    => [ $projectId ]
        }
    )->all();

我得到了所有预期的行,但是如果我执行这在我的 SQLlite 内存数据库中。如果我使用 MySQL 数据库,我会得到完整的数据集。

绑定参数不用于向视图传递任何内容。 事实上,您甚至不需要此视图,只需要两个表 projectproject_additions 的结果 class 并通过它的主键找到项目,然后加入或预取关系到project_additions 结果。 如果您使用两个表的结果 classes 进行搜索,它将生成与您的视图定义完全相同的查询:

$schema->resultset('Project')->find($projectId, { prefetch => 'Project_Addtitions' });

该视图的问题在于视图定义中的 SQL 查询。 LEFT JOIN 中的参数用双引号引起来,即 interpreted as keyword in SQLite.

我改变了:

LEFT JOIN PROJECT_ADDITIONS pp ON p.PROJECT_ID = pp.PROJECT_ID AND pp.FIELD = "ORDER_TYPE"

进入:

LEFT JOIN PROJECT_ADDITIONS pp ON p.PROJECT_ID = pp.PROJECT_ID AND pp.FIELD = 'ORDER_TYPE'

它按预期工作。