Skip to content
This repository has been archived by the owner on Jun 18, 2022. It is now read-only.

Configurable returning columns #17

Closed
potykion opened this issue Nov 12, 2019 · 1 comment
Closed

Configurable returning columns #17

potykion opened this issue Nov 12, 2019 · 1 comment
Milestone

Comments

@potykion
Copy link
Owner

Instead of manually overriding insert:

    async def insert(self, entity: Task) -> Task:
        query = (
            self.table.insert()
            .values(self.serialize(entity))
            .returning(self.table.c.id, self.table.c.priority)
        )
        rows = await self.connection.execute(query)
        row = await rows.first()

        entity.id = row.id
        entity.priority = row.priority

        return entity

You should set up returning columns as prop:

class TaskRepo(BaseRepository[Task]):
    table = tasks_table
    set_on_insert = ("id", "priority")
@potykion
Copy link
Owner Author

The same true for serialize:

    def serialize(self, entity: Task) -> Dict:
        if entity.priority == 0:
            exclude = ["id", "priority"]
        else:
            exclude = ["id"]

        return model_to_primitive(entity, exclude=exclude)

Should be:

class TaskRepo(BaseRepository[Task]):
    table = tasks_table
    ignore_or_insert = ("id", "priority")

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant