emmett-framework / emmett

The web framework for inventors
Other
1.05k stars 71 forks source link

Custom (plural) name for relationships #463

Closed robinvandernoord closed 1 year ago

robinvandernoord commented 1 year ago

According to the documentation, custom plurals are allowed ("mice" instead of "mouses"). Some of my tables are in another language (Dutch) so using the default plural logic of -s is not desired.

However, when I tried to

# works:
class User(BaseModel):
    name = Field()
    has_many(
        'memberships',
        {'groups': {'via': 'memberships'}}
    )

# does not work:
class Group(BaseModel):
    name: str = Field()
    has_many(
        'memberships',
        {'my_users': {'via': 'memberships', "target": "User"}}  # works when it's just users
    )

class Membership(BaseModel):
    belongs_to(
        'user',
        'group'
    )

# fine:
db.User(id=1).groups()
# error:
db.Group(id=1).my_users()

image

What am I doing wrong?

gi0baro commented 1 year ago

You just need to specify the relevant belongs_to in the via:

class Group(BaseModel):
    name: str = Field()
    has_many(
        'memberships',
        {'my_users': {'via': 'memberships.user'}}
    )