adminjs icon indicating copy to clipboard operation
adminjs copied to clipboard

Extend reference with reference key

Open wojtek-krysiak opened this issue 4 years ago • 6 comments

Right now many to one relationship is handled that way: property which is a foreignKey gets type "reference" and system tries to use this propety to find relation by primeryKey. So let say we have User collection and Post collection.

Post has Property (BaseProperty) userId which is a reference set to "User". So when system populates records it knows that after fetching Posts he hast to populate userId with User model.

But this logic doesn't handle a keys when we have a reference to a different key than a primary. So lets suppose that in our example Posts have property accountId and it is reference to User but not to User.id but to User.accountId. In current implementation handling this is not possible.

In this task we will have to make it happen

How to do this? We simply have to return from BaseProperty#reference not a stiring but an object: { model: // old reference externaKey?: // when not set - it is primary key }

Also, we will have to allow people to change that in PropertyOptions and change a way how Populator and Filter work.

Blocker: #251

wojtek-krysiak avatar Feb 27 '20 13:02 wojtek-krysiak

unfortunately this is bigger task. We moved populator logic to the core. Now it has 2 methods for searching: findOne and findMany - both finds by primary key. In order to implement this feature we will have to allow all adapters to find by many for any particular field (not just the primary key)

wojtek-krysiak avatar Sep 26 '20 15:09 wojtek-krysiak

I assume the only current way to get this to work currently is with a custom component that does the querying manually?

Is there any short-term plans for this to be implemented?

AshotN avatar Dec 22 '22 07:12 AshotN

+1

bulbazavr1024 avatar Dec 22 '22 15:12 bulbazavr1024

@wojtek-krysiak any updates ?

bulbazavr1024 avatar Jan 09 '23 08:01 bulbazavr1024

+1

juanmunoz22-bit avatar Feb 17 '23 04:02 juanmunoz22-bit

+1

eduardohferreiras avatar Mar 18 '24 17:03 eduardohferreiras