ORLite:为什么我收到错误消息“找不到对象方法”创建“via package ...”

时间:2012-02-02 13:02:37

标签: sql perl model dbi

为什么我收到错误消息?

#!/usr/bin/env perl
use warnings;
use 5.012;

use ORLite {
    package     => 'My::ORM',
    file        => 'sqlite_test.db',
    create      => sub {
        my $dbh = shift;
        $dbh->do('CREATE TABLE user ( name TEXT NOT NULL, age INTEGER )');
    },
};

say My::ORM::User->table;

my $user = My::ORM::User->create( name => 'Alpha', age  => 23, );

输出:

user
Can't locate object method "create" via package "My::ORM::User" at ./perl1.pl line 16.

1 个答案:

答案 0 :(得分:3)

您正在尝试使用仅在具有PRIMARY KEY的表上可用的方法。

修复创建查询,这将起作用:

$dbh->do('CREATE TABLE user ( name TEXT PRIMARY KEY, age INTEGER )');
#                                       ^^^^^^^^^^^