Many-to-many associations work incorrectly when there is similar foreign key
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 42/100
Direzione di ricerca
Start with the SQLite reproduction and AuthorRepo#all_with_books, focusing on the through association combined by root.combine(books: :book_authors). Trace the association query and mapping behavior, then rerun the script to confirm that Author 1 includes book 1 and that the other results remain correct.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Describe the bug
I have table books with many-to-many relationship with authors via book_authors. However, I also have a column author_id on books to denote the main author.
When querying associations using through:, rom-sql uses the column author_id to combine the associations when query, results in incorrect output.
To Reproduce
Run the below script
require 'rom'
rom = ROM.container(:sql, 'sqlite::memory') do |config|
config.default.create_table(:books) do
primary_key :id
foreign_key :author_id, :authors
end
config.default.create_table(:authors) do
primary_key :id
end
config.default.create_table(:book_authors) do
primary_key :id
foreign_key :book_id, :books
foreign_key :author_id, :authors
end
config.relation(:books) do
schema(infer: true) do
associations do
has_many :book_authors
end
end
auto_struct true
end
config.relation(:authors) do
schema(infer: true) do
associations do
has_many :books, through: :book_authors
end
end
auto_struct true
end
config.relation(:book_authors) do
schema(infer: true) do
associations do
belongs_to :book
belongs_to :author
end
end
auto_struct true
end
end
class AuthorRepo < ROM::Repository[:authors]
def all_with_books
root.combine(books: :book_authors).to_a
end
end
gateway = rom.gateways[:default]
author_repo = AuthorRepo.new(container: rom)
gateway[:authors].insert(id: 1)
gateway[:authors].insert(id: 2)
gateway[:books].insert(id: 1, author_id: 2)
gateway[:books].insert(id: 2, author_id: 2)
gateway[:book_authors].insert(id: 1, book_id: 1, author_id: 1)
gateway[:book_authors].insert(id: 2, book_id: 2, author_id: 2)
gateway[:book_authors].insert(id: 3, book_id: 1, author_id: 2)
puts author_repo.all_with_books.inspect
# output:
# [#<ROM::Struct::Author id=1 books=[]>, #<ROM::Struct::Author id=2 books=[#<ROM::Struct::Book id=1 author_id=2 book_authors=[#<ROM::Struct::BookAuthor id=1 book_id=1 author_id=1>, #<ROM::Struct::BookAuthor id=3 book_id=1 author_id=2>]>, #<ROM::Struct::Book id=1 author_id=2 book_authors=[#<ROM::Struct::BookAuthor id=1 book_id=1 author_id=1>, #<ROM::Struct::BookAuthor id=3 book_id=1 author_id=2>]>, #<ROM::Struct::Book id=2 author_id=2 book_authors=[#<ROM::Struct::BookAuthor id=2 book_id=2 author_id=2>]>]>]
Expected behavior
Author 1 should also have one book from the book_authors record 1
My environment
- Affects my production application: YES
- Ruby version: 3.3
- OS: MacOS
- Lingua principale
- Ruby
- Stelle
- 220
- Fork
- 97
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Preparare l'ambiente
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di rom-rb/rom-sql
-
bug help wanted
Difficoltà 3/5 1-2 giorni Idoneità per principianti 48/100
-
bug help wanted
Difficoltà 3/5 1-2 giorni Idoneità per principianti 42/100
-
bug help wanted
Difficoltà 4/5 3-5 giorni Idoneità per principianti 42/100
-
bug help wanted
Difficoltà 3/5 1-2 giorni Idoneità per principianti 45/100
-
bug help wanted
Difficoltà 4/5 3-5 giorni Idoneità per principianti 35/100
Tutte le issue di rom-rb/rom-sql
Issue simili
-
Bug: Assistant replies lose the space before numbers ("Oct7", "as of10-22-2026") with OpenAI modelsAperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 84/100
we-promise/sure#4441 ·
I maintainer di solito rispondono entro 1 giorno
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
lugassawan/swe-workbench#823 ·
I maintainer di solito rispondono entro 1 giorno
-
[BUG] Transcription spans omit the requested temperatureForse già presa @yorzi l’ha presa oggi. Apertabug
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
crmne/ruby_llm#1276 · 1 reazione ·
I maintainer di solito rispondono entro 1 giorno
-
Admin user page looks up a Tag for every instance-scoped role (`Role#resource_name`)Forse già presa @pkfrank l’ha presa oggi. Apertabug
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
forem/forem#24083 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
rubyjs/mini_racer#449 ·