I'm trying to create an index on a new table that requires uniqueness on the agency_id
(integer) and the IP
address (text). I know that I need to provide a length on the index for IP
. But I'm having issues assigning the length to just the IP
column.
def up
create_table :whitelisted_ips do |t|
t.integer :agency_id
t.text :ip
t.timestamps
end
add_index :whitelisted_ips, [:agency_id, :ip], unique: true, length: 15
end
This code returns the error
Mysql2::Error: Incorrect prefix key; the used key part isn't a string, the used length is longer than the key part, or the storage engine doesn't support unique prefix keys: CREATE UNIQUE INDEX `index_whitelisted_ips_on_agency_id_and_ip` ON `whitelisted_ips` (`agency_id`(15), `ip`(15))
because it is attempting to put the length on the integer field. Any help would be appreciated.
You should provide a hash for the length
parameter instead:
add_index :whitelisted_ips, [:agency_id, :ip], unique: true, length: { ip: 15 }