AuthLogic + PostgreSQL 8.4: persistence_token defined as a string, then used as integer
I'm trying to integrate AuthLogic into my开发者_运维问答 rails application, and I followed the example which defines persistence_token as a string :
https://github.com/binarylogic/authlogic_example
However when I run it with PostgreSQL 8.4 on my ubuntu desktop I get the following error :
ActiveRecord::StatementInvalid in UsersController#index
PGError: ERROR: operator does not exist: character varying = integer
LINE 1: ...* FROM "users" WHERE ("users"."persistence_token" = 21007622...
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
: SELECT * FROM "users" WHERE ("users"."persistence_token" = 2100762299) LIMIT 1
I tried to change persistence_token to an integer, but then it seemed other parts of AuthLogic did not update it.
I'm sure this must be a common problem but googling around was not very helpful, any ideas how to resolve this?
Ruby version: 1.8.7 Rails version: 2.3.5 AuthLogic version: 2.1.6
This is a problem because the rows in your table have a 'persistence_token' value of nil. Setting the column to a value will cause this error to go away. I suspect it has something to do with the way rails inspects the table columns and its interaction with authlogic.
Can you show the code that declares persistence_token?
I'd try type casts and conversions first. On the SQL side, all these will work.
WHERE ("users"."persistence_token" = cast(2100762299 as varchar))
WHERE ("users"."persistence_token" = 2100762299::text)
WHERE ("users"."persistence_token" = text(2100762299))
WHERE ("users"."persistence_token" = 2100762299 || '')
That last one is an implicit type coercion. It forces PostgreSQL to treat the number as a string by concatenating it to an empty string.
But you might have to change the data type at run time in Ruby.
yournumber.to_s
精彩评论