-
-
Notifications
You must be signed in to change notification settings - Fork 119
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
Columns prefixed with table name are not supported #87
Comments
According to error from ClickHouse server you should use different label for the second column with name "user_id". |
I'm blocked too. That seems an anomaly cause as in the nature of sqlalchemy, I'm not supposed to use labellization for the join. Here is the error that I'm getting: Received exception from server (version 20.1.2):
Code: 352. DB::Exception: Received from localhost:XXXX. DB::Exception: Column 'ref_id' is ambiguous. |
OK. Please provide sample of code that reproduces the problem. |
Here is the code that does the job. class Equipments(Base):
"""
Clickhouse model for Equipments
"""
__tablename__ = "equipments"
id = Column(types.Int64, primary_key=True)
product_id = Column(types.Int64, nullable=False)
ref_id = Column(types.Int64, nullable=False)
creation_date = Column(types.DateTime, nullable=False)
__table_args__ = (engines.MergeTree(partition_by=product_id, primary_key=id, order_by=(id, creation_date)),)
class References(Base):
"""
Clickhouse model for References
"""
__tablename__ = "references"
ref_id = Column(types.Int64, primary_key=True)
ref_name = Column(types.Nullable(types.String))
__table_args__ = (engines.MergeTree(partition_by=ref_id, primary_key=ref_id, order_by=(ref_id)),) The file, which contains the base object: import os
from contextlib import contextmanager
from clickhouse_sqlalchemy import make_session
from sqlalchemy import create_engine, MetaData
from sqlalchemy.ext.declarative import declarative_base
CLICK_HOUSE_HOST = os.getenv("CLICK_HOUSE_HOST", "XXXX")
CLICK_HOUSE_USER = os.getenv("CLICK_HOUSE_USER", "XXXX")
CLICK_HOUSE_PASSWORD = os.getenv("CLICK_HOUSE_PASSWORD", "XXXX")
CLICK_HOUSE_DB_NAME = os.getenv("CLICK_HOUSE_DB_NAME", "XXXX")
CLICK_HOUSE_URI = f"clickhouse://{CLICK_HOUSE_USER}:{CLICK_HOUSE_PASSWORD}@{CLICK_HOUSE_HOST}/{CLICK_HOUSE_DB_NAME}"
# SQLAlchemy configuration
ENGINE = create_engine(CLICK_HOUSE_URI)
METADATA = MetaData(bind=ENGINE)
Base = declarative_base(metadata=METADATA)
@contextmanager
def get_db_session():
"""
Return a thread-safe new DB session.
"""
session = make_session(ENGINE)
yield session
session.close() How to reproduce it: from sqlalchemy.orm import Session
def get_value(session: Session):
res = (
session.query(Equipments, References.ref_name)
.join(References, Equipments.ref_id == References.ref_id)
.all()
)
return res
if __name__ == "__main__":
with get_clickhouse_db_session() as session:
res = get_value(session) Here is the query that is runned when I execute my code with SELECT
id AS equipments_id,
product_id AS equipments_product_id,
ref_id AS equipments_ref_id,
creation_date AS equipments_creation_date,
ref_name AS references_ref_name
FROM equipments
INNER JOIN references ON ref_id = ref_id
Received exception from server (version 20.1.2):
Code: 352. DB::Exception: Received from localhost:XXXX. DB::Exception: Column 'ref_id' is ambiguous. |
@xzkostyan That's awesome. Thanks for the fix. |
When join two tables, I found that there are no columns prefixed with table name. However, it is not work for clickhouse with the following error:
Because my SQL have the same column name in two join tables.
The related code in this repository is in /clickhouse-sqlalchemy/drivers/base.py. There are related code:
There is always False for include_table argument in this function.
The text was updated successfully, but these errors were encountered: