piccolo-orm / piccolo

A fast, user friendly ORM and query builder which supports asyncio.
https://piccolo-orm.com/
MIT License
1.46k stars 91 forks source link

Implement `__eq__` on on `Table` #1075

Open Skelmis opened 2 months ago

Skelmis commented 2 months ago

It'd be lovely to be able to compare table instances at a top level without the need to dive down into attributes. Further, users could always override the default id comparison on tables if they do special things.

An example use case of mine:

    @post(
        path="/{project_id:str}/settings/toggle_public_view",
        include_in_schema=False,
    )
    async def toggle_public_view(self, request: Request, project_id: str) -> Redirect:
        project, redirect = await self.get_project(request, project_id)
        if redirect:
            return redirect

        if project.owner != request.user:
            alert(
                request,
                "You must be the project owner to change project visibility.",
                level="error",
            )
            return project.redirect_to()

However with the current behavior the following occurs:

        project.owner.id == request.user.id -> True
        project.owner == request.user -> False
Skelmis commented 2 months ago

An example __eq__ to this is as follows:

    def __eq__(self, other):
        return isinstance(other, self.__class__) and self.id == other.id
dantownsend commented 2 months ago

That's a nice idea - I think it makes sense to add it.