Skip to content
New issue

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

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

Already on GitHub? # to your account

Generator#pk_type: rbs_rails:generate_rbs_for_models fails with composite primary key model #297

Open
casaper opened this issue Feb 7, 2025 · 0 comments

Comments

@casaper
Copy link

casaper commented Feb 7, 2025

Error description

In my project, the rbs_rails:generate_rbs_for_models task fails with my model that has a composite primary key.

It happens with rbs_rails: v0.12.1

Error thrown

NoMethodError with message undefined method 'type' for nil.

Where exactly

RbsRails::ActiveRecord::Generator#pk_type in lib/rbs_rails/active_record.rb:60

pk is at this point ["year", "user_id"] when klass is UserHolidayYear.

      private def pk_type
        pk = klass.primary_key
        return 'top' unless pk

        # pk => `["year", "user_id"]` when `klass.name == 'UserHolidayYear'`
        col = klass.columns.find {|col| col.name == pk }
        sql_type_to_class(col.type)
      end

My model

class UserHolidayYear < ApplicationRecord
  self.primary_key = %i[year user_id]
  # ...
  belongs_to :calendar_year,
             foreign_key: :year,
             primary_key: :year,
             inverse_of: :user_holiday_years
  belongs_to :user
  # ...
end

class CalendarYear < ApplicationRecord
  self.primary_key = :year
  # ...
end

ActiveRecord::Schema[8.0].define(version: 2025_02_06_152444) do
  # ...
  create_table "user_holiday_years", id: false, force: :cascade do |t|
    t.integer "year"
    t.bigint "user_id", null: false
    # ...
    t.index ["year", "user_id"], name: "index_user_holiday_years_on_year_and_user_id", unique: true
  end
  # ...
end
casaper added a commit to casaper/rbs_rails that referenced this issue Feb 7, 2025
…e primary key

Attempting to resolve my own issue: pocke#297

Resolve pocke#297
casaper added a commit to casaper/rbs_rails that referenced this issue Feb 7, 2025
…e primary key

Attempting to resolve my own issue: pocke#297

Resolve pocke#297
casaper added a commit to casaper/rbs_rails that referenced this issue Feb 7, 2025
…e primary key

Attempting to resolve my own issue: pocke#297

Resolve pocke#297
casaper added a commit to casaper/rbs_rails that referenced this issue Feb 7, 2025
…e primary key

Attempting to resolve my own issue: pocke#297

Resolve pocke#297
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant