简体   繁体   中英

Why does DBIx::Class::Schema::Loader change my table name from plural to singular?

I'm using DBIx::Class::Schema::Loader to create a schema like this:

#!/usr/bin/perl

use strict;
use warnings;

use DBIx::Class::Schema::Loader qw/make_schema_at/;

make_schema_at(
    "Mydb::Schema",
    {debug => 0, dump_directory => "../db/",
    generate_pod => 0,},
    ["dbi:mysql:mydb:localhost:3306", 'mydb', 'password'],
);

My table name in MySQL is people , but when I run this code, the generated class is named Mydb::Schema::Result::Person :

$ cat Mydb/Schema/Result/Person.pm 
use utf8;
package Mydb::Schema::Result::Person;

# Created by DBIx::Class::Schema::Loader
# DO NOT MODIFY THE FIRST PART OF THIS FILE

use strict;
use warnings;

use base 'DBIx::Class::Core';
__PACKAGE__->table("people");
__PACKAGE__->add_columns(
  "pplid",
  {
    data_type => "smallint",
    extra => { unsigned => 1 },
    is_auto_increment => 1,
    is_nullable => 0,
  },
...
...

Why is "people" being converted to "Person"?

By default, DBIx::Class::Schema::Loader singularizes the names of Result classes. It makes sense to have a database table of people, but it doesn't make sense to have an object representing a single person called "People".

If you really want, you can change this by setting the naming option in make_schema_at :

make_schema_at(
    "Mydb::Schema",
    {
        debug          => 0,
        dump_directory => "../db/",
        generate_pod   => 0,
        naming         => { monikers => 'preserve' }
    },
    ["dbi:mysql:mydb:localhost:3306", 'mydb', 'password'],
);

But I would recommend sticking with the defaults.

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