Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data with duplicate keys are not loaded #18

Open
tarasovdg opened this issue Jul 7, 2020 · 1 comment
Open

Data with duplicate keys are not loaded #18

tarasovdg opened this issue Jul 7, 2020 · 1 comment

Comments

@tarasovdg
Copy link

A clickhouse may have entries with the same primary keys. And the alchemy in this line eliminates duplicates:

collection = query.all()

For such a case, I think you need to override the all method and use something like session.execute(query).fetchall()

@dmitrymishanov
Copy link
Contributor

dmitrymishanov commented Jun 10, 2021

We found a workaround to fix that.
Define a new data layer class and change Query.all method like that:

class PatchedAll:
    def __init__(self, model):
        self.model = model

    def __get__(self, instance, owner):
        self.instance = instance
        return self

    def __call__(self):
        result = self.instance.session.execute(self.instance).fetchall()
        return [self.model(**{
            k[len(self.model.__tablename__) + 1:]: v for k, v in element.items()
        }) for element in result]


class SQLAlchemyDuplicateDataLayer(SqlalchemyDataLayer):
    def query(self, view_kwargs):
        query = self.session.query(self.model)
        type(self.session.query()).all = PatchedAll(self.model)
        return query

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

No branches or pull requests

2 participants