DBIx::Class如何在创建时检索生成的UUID?

huangapple go评论43阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年4月11日 01:51:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75979451.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定