Catalyst::Plugin::AutoCRUD::Manual::DBICTips - Tips for DBIx::Class Users
If you created your DBIx::Class Schema some time ago, perhaps using an older version of DBIx::Class::Schema::Loader, then it might well be lacking some configuration which is required to get the best results from this plugin.
DBIx::Class
DBIx::Class::Schema::Loader
Common omissions in column configurations include is_foreign_key, join_type, is_nullable, and is_auto_increment. Of course it's also good practice to have your DBIx::Class Schema closely reflect the database schema anyway.
is_foreign_key
join_type
is_nullable
is_auto_increment
To automatically bring things up to date, download the latest version of DBIx::Class::Schema::Loader from CPAN, and use the output from that.
Any column in your result classes which contains the primary key of another table should have the is_foreign_key => 1 option added to its configuration.
is_foreign_key => 1
If using DBIx::Class::Schema::Loader to generate your Schema, use at least version 0.05 or the most recent release from CPAN to have this automatically configured for you.
belongs_to
add_columns
Whenver you use belongs_to() in a result class, it must come after any calls to add_column() which affect the foreign key. A situation where this may not be the case is if you add additional column options in a second call to add_column(), after the DO NOT MODIFY THIS OR ANYTHING ABOVE line.
belongs_to()
add_column()
DO NOT MODIFY THIS OR ANYTHING ABOVE
If you do not follow this guideline, then you won't see any related data in the views generated by this plugin. Furthermore, you'll be losing much of the advantage of DBIx::Class.
A better solution is to re-generate your result class using a recent version of DBIx::Class::Schema::Loader from the CPAN (which may be 0.05 or later).
If you have any belongs_to type relations where the column containing the foreign key can be NULL, it's strongly recommended that you add a join_type parameter to the end of the relevant options to add_columns(), like so:
add_columns()
# in a Book class, the book optionally has an Owner __PACKAGE__->belongs_to( 'my_owner', # accessor name 'My::DBIC::Schema::Owner', # related class 'owner_id', # our FK column (or join condition) { join_type => 'LEFT OUTER' } # attributes );
If you don't do this, some database records will be missing! The technical reason for this, if you are interested, is that DBIx::Class defaults to an INNER join for the belongs_to() relation, but if the column can be null (that is, is_nullable) then you most likely want a LEFT OUTER join.
For those columns where your database uses an auto-incremented value, add the is_auto_increment => 1 parameter to the options list in add_columns(). This will let the plugin know you don't need to supply a value for new or updated records. The interface will look much better as a result.
is_auto_increment => 1
To install Catalyst::Plugin::AutoCRUD, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Catalyst::Plugin::AutoCRUD
CPAN shell
perl -MCPAN -e shell install Catalyst::Plugin::AutoCRUD
For more information on module installation, please visit the detailed CPAN module installation guide.