Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
DBIx::Class unique constraint with limited key length

Greetings wise brothers. I seek your wisdom on how to be confident that all are different without the need to remember every part.

I am working DBIx::Class, and I would like to add a unique constraint of limited key length to a much longer varchar column

My DBIC schema definition code looks like this:

__PACKAGE__->table("classification_rules"); __PACKAGE__->add_columns( "id", { data_type => "integer", is_auto_increment => 1, is_nullable => 0 + }, ... "path", { data_type => "varchar", is_nullable => 0, size => 600 }, ); __PACKAGE__->set_primary_key("id"); __PACKAGE__->add_unique_constraint( ['path'] );

This generates MySQL like this:

CREATE TABLE `classification_rules` ( `id` integer NOT NULL auto_increment, ... `path` text NOT NULL, PRIMARY KEY (`id`), UNIQUE `classification_rules_path` (`path`) ) ENGINE=InnoDB;

The problem is, that when I deploy, MySQL correctly reports that you can not have a unique constraint on a varchar that long without limiting the key length. What I would like to do, is have it generate SQL like UNIQUE `classification_rules_path` (path(250)) but I cannot work out how.

I have already stepped through the add_unique_constraint code in DBIx::Class::ResultSource to try to find an undocumented feature or hook I can use and also added breakpoints in SQL::Translator::Producer::MySQL to try and spot where the SQL is being generated from the other end.

Is there another solution to this? Is there a hook to add literal SQL to the definition for my table?

Thank you.

In reply to DBIx::Class unique constraint with limited key length by chrestomanci

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others imbibing at the Monastery: (8)
    As of 2020-01-23 16:17 GMT
    Find Nodes?
      Voting Booth?