Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用Alembic在SqlAlchemy中定义索引_Python_Sqlalchemy_Alembic - Fatal编程技术网

Python 使用Alembic在SqlAlchemy中定义索引

Python 使用Alembic在SqlAlchemy中定义索引,python,sqlalchemy,alembic,Python,Sqlalchemy,Alembic,我完全同意这是一个相当基本的问题,也许它在某个地方有答案,但不知何故我找不到答案。(我对炼金术也不是很精通) 我有这个密码-- 我怎么能 在这上面定义一个单列索引(比如在电子邮件上)?(可能是 index=True可以做到这一点。如果是这样,那么它主要是下一点 我迷路的地方) 如何在此基础上定义多列索引(例如,在电子邮件和ip地址上。我正在使用alembic进行迁移,当我在类中的列定义之后定义-Index('user\u Index',Users.c.email)[注释行]时,它确实会给我一个错

我完全同意这是一个相当基本的问题,也许它在某个地方有答案,但不知何故我找不到答案。(我对炼金术也不是很精通)

我有这个密码--

我怎么能

  • 在这上面定义一个单列索引(比如在电子邮件上)?(可能是 index=True可以做到这一点。如果是这样,那么它主要是下一点 我迷路的地方)
  • 如何在此基础上定义多列索引(例如,在电子邮件和ip地址上。我正在使用alembic进行迁移,当我在类中的列定义之后定义-Index('user\u Index',Users.c.email)[注释行]时,它确实会给我一个错误,“NameError:name'Users'未定义”)
  • 在alembic的env.py中,除了所有普通行和默认行之外,我还有这两行

    from tutorial.models import Base
    .
    .
    .
    target_metadata = Base.metadata
    
    我的应用程序名是tutorial。如果需要的话,我会用金字塔作为框架。和postgres作为db

    我再次告诉你,这可能是一个非常基本的问题,但我现在无法解决,因此任何帮助都将是巨大的


    谢谢。

    对于我,是的,在字段定义中使用
    index=True

    对于II,只需将
    索引
    声明置于类定义之外:

    class Users(Base):
        __tablename__ = "user_table"
    
        id = Column(Integer, Sequence('idseq'), primary_key=True)
        email = Column(String, unique=True)
        ip_addr = Column(String)
        created_date = Column(TIMESTAMP(timezone=True), default=datetime.datetime.utcnow)
        modified_date = Column(TIMESTAMP(timezone=True), default=datetime.datetime.utcnow)
    
    Index('user_index', Users.c.email, Users.c.ip_addr)
    

    谢谢,它确实起作用了,只是换了点零钱。我没有使用索引('user\u Index',Users.c.email,Users.c.ip\u addr),而是使用了索引('user\u Index',Users.email,Users.ip\u addr),它工作起来很有魅力!!谢谢
    class Users(Base):
        __tablename__ = "user_table"
    
        id = Column(Integer, Sequence('idseq'), primary_key=True)
        email = Column(String, unique=True)
        ip_addr = Column(String)
        created_date = Column(TIMESTAMP(timezone=True), default=datetime.datetime.utcnow)
        modified_date = Column(TIMESTAMP(timezone=True), default=datetime.datetime.utcnow)
    
    Index('user_index', Users.c.email, Users.c.ip_addr)