Postgresql Sqlautocode生成的数据模型:';RelationshipProperty';对象没有属性';c';

Postgresql Sqlautocode生成的数据模型:';RelationshipProperty';对象没有属性';c';,postgresql,sqlalchemy,Postgresql,Sqlalchemy,使用PGModeler,我们创建了一个模式,然后导出了一些适当的SQL代码。SQL命令能够在我们的Postgres数据库中填充适当的表和行 从这里开始,我们想要创建声明性的Sqlalchemy模型,因此使用了Sqlautocode。我们在终点站运行了它: sqlautocode postgresql+psycopg2://username:password@host/db_name -o models.py -d 它按预期生成了我们的表和相应的模型。到目前为止,没有错误 然后,当转到ipyth

使用PGModeler,我们创建了一个模式,然后导出了一些适当的SQL代码。SQL命令能够在我们的Postgres数据库中填充适当的表和行

从这里开始,我们想要创建声明性的Sqlalchemy模型,因此使用了Sqlautocode。我们在终点站运行了它:

sqlautocode postgresql+psycopg2://username:password@host/db_name -o models.py -d
它按预期生成了我们的表和相应的模型。到目前为止,没有错误

然后,当转到ipython时,我从models.py导入了所有内容,并尝试创建在那里定义的类的实例。突然,我发现了这个错误:

AttributeError: 'RelationshipProperty' object has no attribute 'c'
这件事让我困惑了一会儿。其他讨论此问题的SO线程的解决方案与我的问题相差甚远(通常与sqlautocode未使用的特定框架或语法相关)


找到原因后,我决定将手头的问题记录下来。请参见下文。

我们的问题仅仅是因为运行sqlautocode时给变量的命名不正确。具体地说,错误命名发生在任何本身具有外键的模型上

下面是一个例子:

#Note that all \"relationship\"s below are now \"relation\"
#it is labeled relationship here because I was playing around...
service_catalog = Table(u'service_catalog', metadata,
    Column(u'id', BIGINT(), nullable=False),
    Column(u'uuid', UUID(), primary_key=True, nullable=False),
    Column(u'organization_id', INTEGER(), ForeignKey('organization.id')),
    Column(u'type', TEXT()),
    Column(u'name', TEXT()),
    Column(u'parent_service_id', BIGINT(), ForeignKey('service_catalog.id')),
)

#Later on...

class ServiceCatalog(DeclarativeBase):
    __table__ = service_catalog


    #relation definitions
    organization = relationship('Organization', primaryjoin='ServiceCatalog.organization_id==Organization.id')
    activities = relationship('Activity', primaryjoin='ServiceCatalog.id==ActivityService.service_id', secondary=activity_service, secondaryjoin='ActivityService.activity_id==Activity.id')
    service_catalog = relationship('ServiceCatalog', primaryjoin='ServiceCatalog.parent_service_id==ServiceCatalog.id')
    organizations = relationship('Organization', primaryjoin='ServiceCatalog.id==ServiceCatalog.parent_service_id', secondary=service_catalog, secondaryjoin='ServiceCatalog.organization_id==Organization.id')
在ServiceCatalog.organizations中,它希望辅助表为service_catalog,但该变量只是在本地被覆盖。切换两者的顺序将解决此问题