如何使用sqlalchemy重命名声明性表的索引

时间:2019-11-07 08:48:10

标签: postgresql sqlalchemy

我使用sqlacodegen生成数据库的模型代码(SQL Server)。之后,我想将架构和数据迁移到PostgreSQL。 sqlacodegen使用声明式样式定义表。不幸的是,有些表的索引名非常长。例如:


from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class Table_A(Base):
    __tablename__ = 'Table_A'
    __table_args__ = (Index('Really_long_index_that_exceeds_maximum_length_of_63_characters', unique=True), {'schema': 'dbo'})

然后,我要在PostgreSQL中创建表:

import sqlalchemy as db
from models import Base

postgres_engine = db.create_engine(postgres_connection_string)
Base.metadata.create_all(postgres_engine)

不幸的是,这引发了以下错误:

IdentifierError: Idetifier 'Really_long_index_that_exceeds_maximum_length_of_63_characters' exceeds maximum length of 63 characters

我了解到PostgreSQL会自动截断超过63个字符的标识符名称。但是,似乎sqlalchemy无法处理此问题。由于我是自动生成表,因此手动编辑标识符是不切实际的。不幸的是,我找不到任何内置方法,任何属性来访问索引并对其重命名。

那么,有人知道如何使用sqlalchemy中的内置方法来编辑/重命名声明性表的标识符吗?

0 个答案:

没有答案