hanami / model

Ruby persistence framework with entities and repositories
http://hanamirb.org
MIT License
445 stars 152 forks source link

Repository::find doesn't seem to work with composite keys #432

Open graywolf opened 7 years ago

graywolf commented 7 years ago

Assuming I have table containing composite primary key

primary_key [:name, :version]

column :name, String
column :version, String

I didn't find a way to retrieve such record using Repository.new.find. Tried this

repository.find({ name: n, version: v })

but got

Hanami::Model::Error: ROM:Relation[Statuses]#by_pk arity is 2 (1 args given)

Is there a way to retrieve it? Don't seem like it based on source code (afaict)... So how am I supposed to use composite keys? Thanks :)

mereghost commented 7 years ago

ROM can deal with composite keys but it seems that hanami-model can't deal with them right now.

graywolf commented 7 years ago

If ROM can deal with it, would changing

https://github.com/hanami/model/blob/ff2dae480a5736b2dfd6c4a0f9bb6ef9e1aec94f/lib/hanami/repository.rb#L411

to

root.by_pk(*id).as(:entity).one

suffice (source)? It would than be called

repository.find([name, version])

or

repository.find(name, version)

I would prefer the first one imho. But supporting syntax like

repository.find({ name: n, version: v })

would be best but no longer one-line change; I'm sure it can be implemented however I have no idea where to start.

AlfonsoUceda commented 7 years ago

@mereghost what do you think? can you implement this?

AlfonsoUceda commented 7 years ago

Working on this.

MichalJaneda commented 3 years ago

@AlfonsoUceda are you still working on it? If not, may I try to take this over?

adam12 commented 3 years ago

@MichalJaneda they removed their assignment so you're probably safe to work on it,

AlfonsoUceda commented 3 years ago

Hey @MichalJaneda yes you can work on that, but better confirm with @jodosha if it worths it