如何使用 Class::DBI->sequence() 方法在 perl 中自动填充 'id' 字段?
How do I use the Class::DBI->sequence() method to fill 'id' field automatically in perl?
我正在效仿 Class::DBI。
我在我的 MariaDB 数据库中创建了 cd table:
CREATE TABLE cd (
cdid INTEGER PRIMARY KEY,
artist INTEGER, # references 'artist'
title VARCHAR(255),
year CHAR(4)
);
主键cdid没有设置为自增。我想在 MariaDB 中使用一个序列。所以,我配置了序列:
mysql> CREATE SEQUENCE cd_seq START WITH 100 INCREMENT BY 10;
Query OK, 0 rows affected (0.01 sec)
mysql> SELECT NEXTVAL(cd_seq);
+-----------------+
| NEXTVAL(cd_seq) |
+-----------------+
| 100 |
+-----------------+
1 row in set (0.00 sec)
并设置 Music::CD class 以使用它:
Music::CD->columns(Primary => qw/cdid/);
Music::CD->sequence('cd_seq');
Music::CD->columns(Others => qw/artist title year/);
之后,我尝试插入:
# NORMAL INSERT
my $cd = Music::CD->insert({
cdid => 4,
artist => 2,
title => 'October',
year => 1980,
});
# SEQUENCE INSERT
my $cd = Music::CD->insert({
artist => 2,
title => 'October',
year => 1980,
});
“正常插入”成功,但“序列插入”报错:
DBD::mysql::st execute failed: You have an error in your SQL syntax; check the manual that
corresponds to your MariaDB server version for the right syntax to use near ''cd_seq')' at line
1 [for Statement "SELECT NEXTVAL ('cd_seq')
"] at /usr/local/share/perl5/site_perl/DBIx/ContextualFetch.pm line 52.
我认为引号 ('') 引起了错误,因为当我在 mysql 中输入命令“SELECT NEXTVAL (cd_seq)”(不带引号)时客户端它工作(见上文)。我证明了所有的组合(', ", `, 没有引号),但是还是...
有什么想法吗?
我的版本:perl 5.30.3、10.5.4-MariaDB
documentation for sequence()
是这样说的:
If you are using a database with AUTO_INCREMENT (e.g. MySQL) then you do not need this, and any call to insert() without a primary key specified will fill this in automagically.
MariaDB 基于 MySQL。因此您不需要调用 sequence()
。请改用 table 定义中的 AUTO_INCREMENT 关键字。
我正在效仿 Class::DBI。
我在我的 MariaDB 数据库中创建了 cd table:
CREATE TABLE cd (
cdid INTEGER PRIMARY KEY,
artist INTEGER, # references 'artist'
title VARCHAR(255),
year CHAR(4)
);
主键cdid没有设置为自增。我想在 MariaDB 中使用一个序列。所以,我配置了序列:
mysql> CREATE SEQUENCE cd_seq START WITH 100 INCREMENT BY 10;
Query OK, 0 rows affected (0.01 sec)
mysql> SELECT NEXTVAL(cd_seq);
+-----------------+
| NEXTVAL(cd_seq) |
+-----------------+
| 100 |
+-----------------+
1 row in set (0.00 sec)
并设置 Music::CD class 以使用它:
Music::CD->columns(Primary => qw/cdid/);
Music::CD->sequence('cd_seq');
Music::CD->columns(Others => qw/artist title year/);
之后,我尝试插入:
# NORMAL INSERT
my $cd = Music::CD->insert({
cdid => 4,
artist => 2,
title => 'October',
year => 1980,
});
# SEQUENCE INSERT
my $cd = Music::CD->insert({
artist => 2,
title => 'October',
year => 1980,
});
“正常插入”成功,但“序列插入”报错:
DBD::mysql::st execute failed: You have an error in your SQL syntax; check the manual that
corresponds to your MariaDB server version for the right syntax to use near ''cd_seq')' at line
1 [for Statement "SELECT NEXTVAL ('cd_seq')
"] at /usr/local/share/perl5/site_perl/DBIx/ContextualFetch.pm line 52.
我认为引号 ('') 引起了错误,因为当我在 mysql 中输入命令“SELECT NEXTVAL (cd_seq)”(不带引号)时客户端它工作(见上文)。我证明了所有的组合(', ", `, 没有引号),但是还是...
有什么想法吗?
我的版本:perl 5.30.3、10.5.4-MariaDB
documentation for sequence()
是这样说的:
If you are using a database with AUTO_INCREMENT (e.g. MySQL) then you do not need this, and any call to insert() without a primary key specified will fill this in automagically.
MariaDB 基于 MySQL。因此您不需要调用 sequence()
。请改用 table 定义中的 AUTO_INCREMENT 关键字。