Strangely does DBIx change table names from MySQL?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Strangely does DBIx change table names from MySQL?


_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
Rajeev Prasad
Hello,

Note: this question is also posted on Stack Overflow, a few minutes back.


While using ./create-schema-mydb.pl I realized that the table name "People" is changed to "Person" in DBIx. I am not sure how? or why?

It is not even a reserved word.


in MySQL:
people innoDB utf8


create-schema script:
$ cat ./create-schema-mydb.pl
#!/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'],
);


It shows up like this after create-schema... note the change in name from People to Person, but inside the .pm file table name is retained as People !!!
Result$ cat 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,
  },
...
...

only relevant portion shown above...


Thank you.

Rajeev
Reply | Threaded
Open this post in threaded view
|

Re: Strangely does DBIx change table names from MySQL?


_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
Jorge Gonzalez
DBIC by default follows the convention that table names reflect the entity name in singular. If It detects table names in plural It uses a Lingua::* module to deduce the apropriate singular name, and names the clases with It.

In your case, the table names IS "people", but the generated class name IS "Person", the singular for people.

Admittedly yours IS a bit of an extreme case :-)

This behaviour can be changed, see the docs for DBIC::Schema::Loader and search for an option named like "*inflect*" or something similar (I'm on my cellphone and can't search at the moment :-)

BR
J.

--
Jorge González Villalonga
Ingeniero de Sistemas / Systems Engineer
Red Hat Certified Engineer #140-183-666
Móvil / Cell: (+34) 672 173 200

-----Original Message-----
From: Rajeev Prasad <[hidden email]>
To: "DBIx::Class User and Developer List" <[hidden email]>, "[hidden email]" <[hidden email]>, Mysql Mailing List <[hidden email]>, "[hidden email]" <[hidden email]>
Sent: vie., 10 mar. 2017 17:49
Subject: [Dbix-class] Strangely does DBIx change table names from MySQL?

Hello,

Note: this question is also posted on Stack Overflow, a few minutes back.


While using ./create-schema-mydb.pl I realized that the table name "People" is changed to "Person" in DBIx. I am not sure how? or why?

It is not even a reserved word.


in MySQL:
people innoDB utf8


create-schema script:
$ cat ./create-schema-mydb.pl
#!/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'],
);


It shows up like this after create-schema... note the change in name from People to Person, but inside the .pm file table name is retained as People !!!
Result$ cat 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,
  },
...
...

only relevant portion shown above...


Thank you.

Rajeev
Reply | Threaded
Open this post in threaded view
|

Re: Strangely does DBIx change table names from MySQL?

Darren Duncan
On 2017-03-10 9:12 AM, Jorge Gonzalez wrote:
> DBIC by default follows the convention that table names reflect the entity name
> in singular. If It detects table names in plural It uses a Lingua::* module to
> deduce the apropriate singular name, and names the clases with It.
>
> In your case, the table names IS "people", but the generated class name IS
> "Person", the singular for people.

Semantically this is one of those things I would argue that Entity Framework got
(more) right and DBIC got (more) wrong.

Semantically a table is a collection of rows where typically each row represents
a singular entity, for example a single person, and a table represents a simple
set/bag/array of those entities, and is collectively for example a group of 0..N
people.

I believe table-typed variables such as those in SQL databases should be named
using the same naming conventions that are appropriate for arrays, after what
the whole collection represents, such as "people", whereas row-typed variables
would best be named for what a single row is, such as "person".

As this simple example in SQL (like typical generated SQL) demonstrates:

   select person.name, person.age from people person

Here "people" is the name of the table-typed SQL variable, and "person" is the
name of the SQL range variable declared in the "select" that represents each
individual row in turn.

This is a loose Perl analogy:

   for my $person ($db->people) { print $person->name, $person->age }

And so, DBIC would ideally name any classes/objects/variables representing a
single person as Person while any representing a collection would be People,
which is the behavior that "code first" Entity Framework defaults to.

-- Darren Duncan


_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
Reply | Threaded
Open this post in threaded view
|

Re: Strangely does DBIx change table names from MySQL?

Lasse Makholm


On Fri, Mar 10, 2017 at 8:38 PM, Darren Duncan <[hidden email]> wrote:
On 2017-03-10 9:12 AM, Jorge Gonzalez wrote:
DBIC by default follows the convention that table names reflect the entity name
in singular. If It detects table names in plural It uses a Lingua::* module to
deduce the apropriate singular name, and names the clases with It.

In your case, the table names IS "people", but the generated class name IS
"Person", the singular for people.

Semantically this is one of those things I would argue that Entity Framework got (more) right and DBIC got (more) wrong.

Semantically a table is a collection of rows where typically each row represents a singular entity, for example a single person, and a table represents a simple set/bag/array of those entities, and is collectively for example a group of 0..N people.

I believe table-typed variables such as those in SQL databases should be named using the same naming conventions that are appropriate for arrays, after what the whole collection represents, such as "people", whereas row-typed variables would best be named for what a single row is, such as "person".

As this simple example in SQL (like typical generated SQL) demonstrates:

  select person.name, person.age from people person

Here "people" is the name of the table-typed SQL variable, and "person" is the name of the SQL range variable declared in the "select" that represents each individual row in turn.

This is a loose Perl analogy:

  for my $person ($db->people) { print $person->name, $person->age }

That's sort of what DBIC does already:

my $people = $db->resultset('Person')->search(...);
foreach my $person ($people->all) { ... }

And so, DBIC would ideally name any classes/objects/variables representing a single person as Person while any representing a collection would be People, which is the behavior that "code first" Entity Framework defaults to.

The DBIC result source is named Person and each result (row) object is a Person. You should (in my opinion) read $db->resultset('Person') as "collection of objects of type Person".

DBIC is nice in that way; it doesn't force you to remember when something is plural or singular - it's always singular. If you want to name your resultset $people, you are perfectly free to do so.
 

-- Darren Duncan


_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...


_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...