Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

raises an error for models using enum. #129

Open
krisleech opened this issue Dec 1, 2023 · 1 comment
Open

raises an error for models using enum. #129

krisleech opened this issue Dec 1, 2023 · 1 comment

Comments

@krisleech
Copy link

class Post < ActiveRecord::Base
  enum status: %i[draft published]
end

The error we get is:

Undeclared attribute type for enum 'status'. Enums must be backed by a database column or declared with an explicit type via `attribute`.

Here is a reproducible example:

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  gem 'pry'
  gem 'rspec'
  gem "activerecord", '7.1.2'
  gem 'activerecord-nulldb-adapter', '1.0.1', require: false
end

require "active_record"
require 'nulldb_rspec'
require "rspec/autorun"

schema = <<-SCHEMA
ActiveRecord::Schema[7.1].define do
  create_enum "post_status", ["draft", "published"]

  create_table "posts", force: :cascade do |t|
    t.enum "status", default: "draft", enum_type: "post_status"
  end
end
SCHEMA

schema_filename = File.join(__dir__, 'schema.rb')

File.write(schema_filename, schema)

ActiveRecord::Base.establish_connection(
  adapter: :nulldb,
  schema: schema_filename
)

require_relative 'schema'

class Post < ActiveRecord::Base
  enum status: %i[draft published]
end

RSpec.describe 'enum' do
  it 'has status column' do
    expect(Post.column_names).to include('status')
  end

  it 'does not raise an error' do
    expect { Post.new }.not_to raise_error
  end
end

The exception comes from ActiveRecord::Enum#_enum (gems/activerecord-7.1.2/lib/active_record/enum.rb:250).

attribute(name, **options) do |subtype|
    249:         binding.pry
 => 250:           if subtype == ActiveModel::Type.default_value
    251:             raise "Undeclared attribute type for enum '#{name}'. Enums must be" \
    252:               " backed by a database column or declared with an explicit type" \
    253:               " via `attribute`."
    254:           end

With NullDB subtype, in the above, is the same as ActiveModel::Type.default_value, thus the error:

pry> subtype
=> #<ActiveModel::Type::Value:0x00000001059d25a0 @limit=nil, @precision=nil, @scale=nil>
pry> ActiveModel::Type.default_value
=> #<ActiveModel::Type::Value:0x0000000105935070 @limit=nil, @precision=nil, @scale=nil>

With regular adapter they are different and thus no error:

pry> subtype
=> #<ActiveModel::Type::String:0x000000010c6f2a40 @false="f", @limit=nil, @precision=nil, @scale=nil, @true="t">
pry> ActiveModel::Type.default_value
=> #<ActiveModel::Type::Value:0x000000010bc38258 @limit=nil, @precision=nil, @scale=nil>

It's been great having unit tests use this gem. Any ideas greatly appreciated!

  • nulldb 1.0.1
  • ActiveRecord 7.1.2
@krisleech
Copy link
Author

krisleech commented Dec 1, 2023

@Zeko369 I noticed your PR, did you come across this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant