Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails BLOB/TEXT column used in key specification without a key length

I am trying to run a rails migration and I am seeing the error "BLOB/TEXT column used in key specification without a key length"... However, I'm specifying the length in the migration class. Rails seems to ignore this when generating the SQL statement from the migration. Any clues?

Here's my migration class:

class AddIndexToAccounts < ActiveRecord::Migration
def self.up
  add_index :TACCOUNT, :NAMEX, :length => 5
end
def self.down
  remove_index :TACCOUNT, :NAMEX
end
end

And here's the error output. It seems to have seen the length specification, but it's not present in the SQL statement that it generates:

** [out :: 192.168.10.7] -- add_index(:TACCOUNT, :NAMEX, {:length=>5})
** [out :: 192.168.10.7] rake aborted!
** [out :: 192.168.10.7] An error has occurred, all later migrations canceled:
** [out :: 192.168.10.7] 
** [out :: 192.168.10.7] Mysql::Error: BLOB/TEXT column 'NAMEX' used in key specification without a key length: CREATE  INDEX `index_TACCOUNT_on_NAMEX` ON `TACCOUNT` (`NAMEX`)
like image 228
Morgan H Avatar asked Apr 10 '12 15:04

Morgan H


People also ask

How do I fix mysql error 1170?

The Alter Table SQL command will fail. The solution to the problem is to remove the TEXT or BLOB column from the index or unique constraint, or set another field as primary key. If you can't do that, and wanting to place a limit on the TEXT or BLOB column, try to use VARCHAR type and place a limit of length on it.

What is key length in mysql?

That is, the maximum index key length is 1536 bytes when the page size is 8KB, and 768 bytes when the page size is 4KB. The limits that apply to index key prefixes also apply to full-column index keys. A maximum of 16 columns is permitted for multicolumn indexes.


1 Answers

You could try being more specific to see if that tricks it into working:

add_index :TACCOUNT, :NAMEX, :length => { :NAMEX => 5 }

As a last resort you can crete the index the "hard way" using SQL directly by adjusting the incorrect ADD INDEX statement and using execute on the correct one.

like image 153
tadman Avatar answered Oct 05 '22 22:10

tadman