简体   繁体   中英

ORLite: Why do I get the error-message 'Can't locate object method “create” via package…'

Why do I get the error-message?

#!/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, );

Output:

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

You are trying to use a method which is only available on tables that have a PRIMARY KEY.

Fix the creation query and that will work:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM