Rails migrations with database-specific data types
Asked Answered
P

2

13

I'm currently running a Rails migration where I am adding a datatype specific to Postgres, the tsvector. It holds search information in the form that Postgres expects for its built-in text searching capabilities.

This is the line from my migration:

t.column "search_vectors", :tsvector

Everything seems to be working fine, and the search works with it. However, when I opened up schema.rb, this is what I got:

Could not dump table "users" because of following StandardError
Unknown type 'tsvector' for column 'search_vectors'

This is preventing me from running unit tests on the user table, and also strikes me as really dangerous looking given that the schema.rb is supposed to be the authoritative definition of my database.

I notice there are a number of Rails plugins that seem to use the same approach of storing the tsvector like I would expect, such as tsearchable. Am I really just stuck without testing and without an authoritative definition of my database?

Pontias answered 1/3, 2010 at 22:49 Comment(0)
P
14

FYI for anyone who happens across this page, I fixed this by adding this (actually uncommenting it) to my Rails config:
config.active_record.schema_format = :sql

Pontias answered 4/3, 2010 at 23:40 Comment(0)
C
1

Have you tried specifying the type as a string instead of a symbol?

t.column "search_vectors", "tsvector"

If that doesn't work then you might need to drop down to database-specific SQL:

def self.up
  execute "--Put your PostgreSQL specific SQL statements here"
end
Corollaceous answered 2/3, 2010 at 9:11 Comment(2)
Changing to strings did not fix the problem. However, did you edit your response? I think you previously had a recommendation to change my config to: config.active_record.schema_format = :sql This did work.Pontias
Not me. That advice was deleted by the person who posted it (Phil Ross) for some reason.Corollaceous

© 2022 - 2024 — McMap. All rights reserved.