lapis icon indicating copy to clipboard operation
lapis copied to clipboard

Support passing a model instance instead of a string in relations

Open JDaance opened this issue 3 years ago • 2 comments
trafficstars

image

"Users" here have to be a string, that will be required like this

image

I would like to be able to pass a model instance like this instead when needed:

image

JDaance avatar Mar 02 '22 10:03 JDaance

So the reason why it exists the way it currently does it to avoid issues with circular dependencies. You may not have access to the value of the model yet. (eg. they both have a relation to each other)

Maybe an alternative general purpose solution is to allow using a function in place of the string to lazily load the value of the model instance.

As a work around for you current problem of not wanting to use the forced method of organizing models, you can use package.loaded

package.loaded.models = {
  userTable = self._userTable, -- ..etc
}

Then reference the model as "userTable" in the class declaration.

leafo avatar Mar 02 '22 19:03 leafo

Yes I suspected that it might be due to circular deps!

I would be fine with a function based approach, but I would like even better if it accepted string (current), instance (to use when no circular deps are present) and function. But I can see that the circular situation is very common, declaring the relationship on both sides.

I did a workaround by passing a string as it wants and then override get_relation_model on that specific model (matching on the string).

JDaance avatar Mar 02 '22 20:03 JDaance