clickhouse使用clickhouse-sqlalchemy来创建表关联外键问题
发布于 7 个月前 作者 olei_me 647 次浏览 来自 问答
  • clickhouse的表之间的外键约束,是哪种表引擎的?
  • 我使用clickhouseclickhouse-sqlalchemy包来进行数据库的创建,帮我看看下面的代码中,两表之间关联的地方报错了
# -*- coding: utf-8 -*-

from sqlalchemy import create_engine, Column, MetaData, literal, ForeignKey
from sqlalchemy.orm import relationship
from clickhouse_sqlalchemy import make_session, get_declarative_base, types, engines
from datetime import datetime

uri = "clickhouse://default:Qbb4RJa4@localhost/default"

engine = create_engine(uri)
session = make_session(engine)
metadata = MetaData(bind=engine)

Base = get_declarative_base(metadata=metadata)


class User(Base):
    __tablename__ = "user"
    id = Column(types.Int8, primary_key=True) 
    name = Column(types.String) 
    pwd = Column(types.String)  
    email = Column(types.String)  
    phone = Column(types.String)  
    info = Column(types.String)  
    face = Column(types.String)  
    addtime = Column(types.Date)
    uuid = Column(types.String)  
    userlogs = relationship('Userlog', backref='user') 

    __table_args__ = (
        engines.Memory(),
    )

    def __repr__(self):
        return "<User %r>" % self.name


class Userlog(Base):
    __tablename__ = "userlog"
    id = Column(types.Int8, primary_key=True)
    user_id = Column(types.Int8, ForeignKey('user.id')) 
    ip = Column(types.String)
    addtime = Column(types.Date)
    
	__table_args__ = (
        engines.Memory(),
    )
	
    def __repr__(self):
        return "<Userlog %r>" % self.id


if __name__ == "__main__":
    Base.metadata.create_all(engine)

显示的是: clickhouse-linux.png

回到顶部