Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

DBIx::Class : stop changing table, column, relationship names

by bliako (Monsignor)
on Feb 22, 2023 at 19:50 UTC ( [id://11150536]=perlquestion: print w/replies, xml ) Need Help??

bliako has asked for the wisdom of the Perl Monks concerning the following question:

I am a bit confused as to how to tell DBIx::Class (version 0.082843 on SQLite) not to change ANY table, column, relationship names. The whole lot.

I found the naming key in the parameters of https://metacpan.org/pod/DBIx::Class::Schema::Loader#make_schema_at (version 0.07051) to take values like:

naming => { monikers => 'preserve', relationships => 'preserve', column_accessors => 'preserve', }

This works (AFAICS) except when it creates relationships wtth CamelNames it breaks their hump: camel_names. Surely this is animal cruelty. I tried using all available options for relationships => v4, v5, v6, v7, v8, preserve (See https://metacpan.org/pod/DBIx::Class::Schema::Loader::Base#naming).

Additionally, on trying the different relationships => v4, v5, v6, v7, v8, current there are serious changes in the relationships code other than just the names. For example on 'v4' I get:

__PACKAGE__->belongs_to( "MyRefTableId", "My::Schema::Result::MyRefTable", { "Id" => "MyRefTableId", }, );

and when using 'current' I get:

__PACKAGE__->belongs_to( "MyRefTableId", "My::Schema::Result::MyRefTable", { "Id" => "MyRefTableId", }, { "is_deferrable" => "0", "on_delete" => "NO ACTION", "on_update" => "NO ACTION", }, );

Which, AFAICT, it not only changes the naming conventions but changes the logic!

My DB schema is dictated by JSON from a REST API which I have no control. They use CamelNames. I want to replicate EXACTLY that. I do not want:

  • DBIx::Class to make changes wrt Plural, Singular, and Case.
  • to have the DB schema changing each time DBIx::Class changes version.
  • different relationship behaviour, e.g. the presence or absence of on_update because I change the relationships value UNDER naming key.

So, simple question: how can DBIx::Class et.al. not chnage any names in my SQL create-tables. Not a single iota.

bw, bliako

Replies are listed 'Best First'.
Re: DBIx::Class : stop changing table, column, relationship names
by NERDVANA (Deacon) on Feb 24, 2023 at 07:16 UTC
    I'm not entirely sure what you're saying here. Schema::Loader reads the structure of your database to generate DBIC result classes, and it ignores the names of the foreign keys in your database and makes guesses about what the name of the relationship should be based on the column names involved. If you declare one of those naming conventions, it will generate the same names every time, but it has no idea what the relationship name "should" be.

    If you have relationships defined in JSON, then why are you using Schema::Loader? You could just generate the DBIC result classes however you like. If you want to pick up the database column names, but define custom relationships, you can do that by putting the relationships below the auto-generated code, and Schema::Loader will preserve all that custom code while re-generating the column definitions.

    There is one project where I wanted to retroactively use SchemaLoader, but other developers had already chosen all sorts of names for the relationships, and I needed to preserve that. I ended up writing a very long rel_name_map parameter in my script that runs Schema::Loader to override all its choices of relationship name. i.e.

    rel_name_map => { Group => { reportnum => 'report', rpt_sections => 'sections', },
    and so on for ~50 relationships or so. It was annoying and tedious, but let me choose all the specific names I needed to preserve. I don't recommend this really; since you have them in JSON already you could just tell Schema::Loader to ignore foreign keys and then inject the relationships into the generated code yourself with option filter_generated_code.

      Thanks for your reply. Perhaps I was a bit vague, so here is some more detail.

      No, the JSON data does not contain the relationships. Only table names, and I infer the foreign keys.

      Consider the following SQL of two tables where one is referencing the other's PK:

      CREATE TABLE MyappType ( Id INTEGER NOT NULL UNIQUE # ... ); CREATE TABLE AvailableMyapp ( MyappTypeId INTEGER NOT NULL, FOREIGN KEY(MyappTypeId) REFERENCES MyappType(Id) # ... );

      Using:

      DBIx::Class::Schema::Loader::make_schema_at( 'Myapps::Model::Schema', skip_load_external => 1, naming => { monikers => 'preserve', #relationships => 'current', column_accessors => 'preserve', }, );

      creates following perl in file : Model::Schema::Result::AvailableMyapp

      ... __PACKAGE__->has_many( "available_myapps", "Myapps::Model::Schema::Result::AvailableMyapp", { "foreign.MyappTypeId" => "self.Id", }, { "cascade_copy" => "0", "cascade_delete" => "0", }, );

      and this perl in file 'Model/Schema/Result/AvailableMyapp.pm':

      ... __PACKAGE__->belongs_to( "myapp_type", "Myapps::Model::Schema::Result::MyappType", { "Id" => "MyappTypeId", }, { "is_deferrable" => "0", "on_delete" => "NO ACTION", "on_update" => "NO ACTION", }, );

      My problem is with the relationship names "available_myapps" and "myapp_type". I don't want the plural in the first one and I don't want to break the camel type for either.

        So you really just want the relationship name to be exactly the name of the table it links to? I think that can be done with

        rel_name_map => sub { $_[0]{remote_moniker} }

        To clarify some of your earlier questions, the reason that the moniker options were not having the desired effect is that they only affect the class name itself. They are assuming that most people have lowercase table names that might be plural and they assume that the average person wants to convert those to camel-case perl package names that are singular. They never considered that someone might want camel-case relationship names.

        The reason why most people don't want camel-case relationship names or attribute names is that Perl's naming convention serves an important purpose. There is a namespace clash between a package name A::B and a fully qualified method name A::b and as soon as you start using uppercase method names, you can run into messy name conflicts. You can see this in the documentation of Type::Tiny (or Types::Standard, I forget where exactly) where the author recommends that everyone always call constructors as "A::B"->new instead of  A::B->new because the latter will break if there is a type named "B" imported into the namespace of package "A". Well, the real problem is that Type::Tiny encouraged people to import a bunch of symbols that start with a capital letter and have a high probability of conflicting with a package deeper in the hierarchy, and that those symbols remain in the package if you don't use namespace::clean; and then confuse perl's heuristics about whether you are referring to a package or calling a function. For DBIC relations it probably doesn't matter, because nobody is likely to want to define classes within the namespace of a Result class.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11150536]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-19 12:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found