How do you specify index length when using DBIx::Class?

旧时模样 提交于 2019-12-10 12:18:24

问题


I'm using DBIx::Class and am using the sqlt_deploy_hook to:

sub {
    my ($self, $sqlt_table) = @_;
    $sqlt_table->add_index(name => 'indexes', fields => [keys %for_indexing]);
}

Some of the columns I want to index are of type text, or are varchars > 255 length. MySQL doesn't like text indexes unless you specify them with a length, like this:

index (long_field(996))

But looking at SQL::Translator::Schema::Table (which has the add_index method) and Index, I see no way to specify the length.

add_index(name => 'indexes', fields => ['long_field'])

generates this SQL:

INDEX `indexes` (`long_field`)

and this:

add_index(name => 'indexes', fields => ['long_field(996)'])

generates this SQL:

INDEX `indexes` (`long_field(996)`)

which doesn't work, since there is not column with that name.

Up to now I've gotten around this by simply not indexing my text columns. However I'm now trying to use DBIx::Class::DeploymentHandler which is converting my long varchars to text columns when I "install", and I really need those varchar columns indexed.

What can I do?


回答1:


I'm sorry, SQL::Translator simply doesn't understand how to produce index declarations with lengths, which means that DBIC deploy and DBICDH can't do it either, since they do their work through SQLT. If you want to come by irc.perl.org #sql-translator and discuss how this feature would work and what the interface would be, it might get added, but currently it's not possible.

As for your assertion that DBICDH is "converting my long varchars to text columns", there's something very wrong with that; it's not desired behavior for DBICDH. Please post a new question about that issue, showing what you're doing, or bring it to irc.perl.org #dbix-class.



来源:https://stackoverflow.com/questions/6859955/how-do-you-specify-index-length-when-using-dbixclass

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!