英文:
DBIx::Class how to retrieve generated UUID on create?
问题
我有一个使用DBIx::Class
的应用程序,当前在我创建与表对应的对象时,返回的对象中id
列的值会被设置为0
,而不是数据库级别生成的UUID
。我如何在创建新记录时正确地检索id
列?
这是我的表:
CREATE TABLE IF NOT EXISTS log (
id VARCHAR(36) DEFAULT (UUID()) PRIMARY KEY,
...
这是我的DBIx::Class
模式:
__PACKAGE__->load_components(qw/InflateColumn::DateTime PK::Auto Core/);
__PACKAGE__->table('log');
__PACKAGE__->add_columns(
id => {
data_type => 'varchar',
size => 36,
unique => 1,
is_auto_increment => 1
},
qw/.../ # 其他列
);
__PACKAGE__->set_primary_key('id');
当我要插入数据时,像这样(log_repository
是我的结果集):
my $log = $self->log_repository->create($json);
print $log->get_column('id'); # 返回0而不是有效的UUID
我如何在创建时检索UUID
?
英文:
I have an application that uses DBIx::Class
, currently when I create an object that corresponds to a table I get back the object but the id
column will be set to 0
instead of to the UUID
that's generated on the database level. How can I properly retrieve the id
column when I create a new record?
Here is my table:
CREATE TABLE IF NOT EXISTS log (
id VARCHAR(36) DEFAULT (UUID()) PRIMARY KEY,
...
Here is my DBIx::Class
schema:
__PACKAGE__->load_components(qw/InflateColumn::DateTime PK::Auto Core/);
__PACKAGE__->table('log');
__PACKAGE__->add_columns(
id => {
data_type => 'varchar',
size => 36,
unique => 1,
is_auto_increment => 1
},
qw/.../ # other columns
);
__PACKAGE__->set_primary_key('id');
When I go to insert, like (log_repository
is my resultset):
my $log = $self->log_repository->create($json);
print $log->get_column('id'); # 0 instead of valid UUID
How can I retrieve the UUID
on create?
答案1
得分: 1
I found one way to solve it, though it isn't pretty, and far from what I'd like to do, it works.
In my ResultSet
classes, I over-wrote the create
method to append a pre-generated UUID to the id
field before executing the super-classes' create like so:
sub create {
my $self = shift;
my $qry = shift;
return $self->SUPER::create( { %{$qry}, id => Data::UUID->new->create_str } );
}
Then I can call create
like usual, and get the id
back.
英文:
I found one way to solve it, though it isn't pretty, and far from what i'd like to do, it works.
In my ResultSet
classes, I over-wrote the create
method to append a pre-generated UUID to the id
field before executing the super-classes' create like so:
sub create {
my $self = shift;
my $qry = shift;
return $self->SUPER::create( { %{$qry}, id => Data::UUID->new->create_str } );
}
Then I can call create
like usual, and get the id
back.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论