Getting the following error:
ActiveRecord::StatementInvalid: ActiveRecord::StatementInvalid: PG::UndefinedFunction: ERROR: operator does not exist: uuid <> integer
LINE 1: ...rs"."email" = '[email protected]' AND "users"."id" != 1002) L...
I'm using UUID for my keys, and build_stubbed tries to create my relationship using an integer instead of a uuid.
Anyway to have a setting to decide between an id and a uuid?
build and create work well because they persist on the database the relationships.
Thanks!
@allaire that's interesting - if you're using build_stubbed, it actually shouldn't be hitting the database at all. Ideas why it would be?
@joshuaclayton Not really :/ Maybe ActiveRecord has some kind of info about the column that it's a UUID and not a simple Integer? Or does build_stubbed create the relation persisted to the DB?
@allaire re: casting, that was my first thought, but this looks like it's doing a lookup somewhere; hard to tell due to lack of context, although it looks like it might be going through an association or scope somewhere.
My guess is that the problem's only showing up because of the type difference, and that normally the association/scope/whatever would just return nil or an empty collection instead of raising. I think were FG to allow this, the underlying issue would still be the same (since, if you ARE working with fully stubbed records like this, the id shouldn't matter at all).
Same issus, here is the details.
When validate with the unique it will look up the database. So the 1xxx id is wrong.
Here is my quick fix.
require 'factory_girl_rails'
module FactoryGirl
module Strategy
class Stub
private
def next_id
SecureRandom.uuid
end
end
end
end
I'm not very good at ruby. So just leave the message here. Hope next version could fix it.
Thank you @jerry-tao, that snippet worked perfectly, I/we just ran into the same issue.
Are there any plans to support UUIDs? I'd be up to work on a PR for this.
Just ran into this too. Thanks for the workaround @jerry-tao.
I'd welcome a PR that provides FG configuration for a class whose responsibility is to generate the next id for build_stubbed. Closing this for now.
I've tried out the following, to keep the change at a factory level. I think the suggested monkeypatch above changes all factories, right?
after(:build_stubbed) do |record|
record.id = SecureRandom.uuid
end
I ran into this today. Just wanted to mention that the workaround provided by @valer-cara above does work as long as you use after(:stub) instead of after(:build_stubbed) since after(:stub) is the appropriate callback to use when using build_stubbed as per https://github.com/thoughtbot/factory_bot/blob/master/GETTING_STARTED.md#callbacks
Most helpful comment
I've tried out the following, to keep the change at a factory level. I think the suggested monkeypatch above changes all factories, right?