[Dbix-class] Strangely does DBIx change table names from MySQL?
Darren Duncan
darren at darrenduncan.net
Fri Mar 10 19:38:56 GMT 2017
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
More information about the DBIx-Class
mailing list