ActiveRecordRelations cannot generate an RBI for an Active Record model with a composite primary key
Maintainers usually reply within 3 days
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 76/100
Research direction
Start in lib/tapioca/dsl/compilers/active_record_relations.rb around line 801 and inspect ActiveRecordColumnTypeHelper#type_for in lib/tapioca/dsl/compilers/helpers/active_record_column_type_helper.rb around line 60. Run the supplied repro.rb with Rails 8 and PostgreSQL available, then verify generation succeeds for the composite key and produces typed tuple ids while preserving scalar primary-key behavior.
Written by the indexing model from the issue text.
Description
Problem
ActiveRecordRelations cannot generate an RBI for an Active Record model with a composite primary key. When generating ids, it passes
constant.primary_key—an Array—to ActiveRecordColumnTypeHelper#type_for, whose argument is typed as String.
Reproduced with Tapioca 0.19.2 and Rails 8. The same call is present in v0.20.0 and current main, though I have only run this reproduction against
0.19.2.
Minimal reproduction
With tapioca 0.19.2, Active Record 8, and the PostgreSQL adapter available, run bundle exec ruby repro.rb:
require "active_record"
require "active_record/connection_adapters/postgresql/oid/uuid"
require "tapioca/internal"
require "tapioca/dsl/compilers/active_record_relations"
class CompositeRecord < ActiveRecord::Base
self.primary_key = %w[listing_id checkin_on checkout_on]
def self.table_exists? = true
def self.columns_hash
%w[listing_id checkin_on checkout_on].to_h do |name|
[name, Struct.new(:null).new(false)]
end
end
def self.attribute_types
{
"listing_id" => ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Uuid.new,
"checkin_on" => ActiveRecord::Type::Date.new,
"checkout_on" => ActiveRecord::Type::Date.new,
}
end
end
compiler = Tapioca::Dsl::Compilers::ActiveRecordRelations
pipeline = Tapioca::Dsl::Pipeline.new(
requested_constants: [CompositeRecord],
requested_compilers: [compiler],
)
root = RBI::Tree.new
compiler.new(pipeline, root, CompositeRecord).decorate
The metadata is supplied in memory to keep the reproduction database-free; it exercises the actual compiler.
Actual result
TypeError: Parameter 'attribute_name': Expected type ::String,
got type Array with value ["listing_id", "checkin_on", "checkout_on"]
Caller: .../active_record_relations.rb:801
Definition: .../active_record_column_type_helper.rb:60
Expected result
Generation succeeds, with ids returning typed tuples for these columns—for example, T::Array[[::String, ::Date, ::Date]].
ActiveRecordColumnTypeHelper#type_for("id") already delegates to its composite-aware id_type method. Could the Relations compiler use that path for ids,
while preserving scalar/nonstandard primary-key behavior?
Related earlier work: #1966 addresses composite find overloads; #2007 addresses composite column types.
- Dominant language
- Ruby
- Stars
- 874
- Forks
- 165
- Avg merge
- 5d 2h
- Merged PRs (30d)
- 12
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- No contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from Shopify/tapioca
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Shopify/tapioca#2662 · 1 comment ·
Maintainers usually reply within 3 days
-
Improve `check-shims` performancePossibly taken @KaanOzkan claimed this 8 days ago. Open
Shopify/tapioca#2728 · 1 assignee ·
Maintainers usually reply within 3 days
-
Difficulty 5/5 Over a week Newbie friendliness 30/100
Maintainers usually reply within 3 days
-
Prepending a module can cause `tapioca dsl` to produce an untyped signaturePossibly taken @KaanOzkan claimed this 43 days ago. Openbug
Shopify/tapioca#2705 · 1 assignee ·
Maintainers usually reply within 3 days
-
Difficulty 3/5 1-2 days Newbie friendliness 68/100
Shopify/tapioca#2663 · 2 comments ·
Maintainers usually reply within 3 days
Similar issues
-
Add ZammadPossibly taken @Arslan-TR claimed this today. Openrequest
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
endoflife-date/endoflife.date#11298 · 1 comment ·
Maintainers usually reply within 1 day
-
performance v/6
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
WITCodingClub/calendar-backend#689 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 1/5 Under an hour Newbie friendliness 86/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
rubys/roundhouse#571 ·
Maintainers usually reply within 1 day
-
area/web interface
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
mastodon/mastodon#40924 · 1 comment ·
Maintainers usually reply within 1 day