sqlalchemy两个基类

时间:2017-03-13 02:53:48

标签: python sqlalchemy

我希望我的子类继承自两个基类

from sqlalchemy import * 
from sqlalchemy.ext.declarative import as_declarative,declared_attr,AbstractConcreteBase
import re
import datetime
from sqlalchemy.orm import relationship,Session, relationship, backref,Mapper

def camel_case_split(identifier):
    matches = re.finditer('.+?(?:(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])|$)', identifier)
    return [m.group(0) for m in matches]

@as_declarative()
class ModelBase1(object):
"""
"""
    @declared_attr
    def __tablename__(cls):
       name_list=camel_case_split(cls.__name__)
       return '_'.join([s.lower() for s in name_list])
    id=Column(Integer, primary_key=True)

@as_declarative()
class ModelBase2(object):
"""
"""
    @declared_attr
    def __tablename__(cls):
        name_list=camel_case_split(cls.__name__)
        return '_'.join([s.lower() for s in name_list])

    id=Column(Integer, primary_key=True)
    name=Column(String(32))

class User(ModelBase2):
'''
'''
    group_id=Column(String(32),ForeignKey('channel.id'),nullable=True)
    group=relationship('UserGroup',foreign_keys=[group_id], backref='users')

class UserGroup(ModelBase1):
    group_access=Column(String(32),nullable=True)

if __name__ == "__main__" and __package__ is None:
    user1=User(name='mxin')
    print user1.name

当我运行此脚本时,会出现如下错误: sqlalchemy.exc.InvalidRequestError:初始化映射器Mapper | User | user,expression&#39; UserGroup&#39;找不到姓名(&#34;名称&#39; UserGroup&#39;未定义&#34;)。如果这是一个类名,请考虑在定义了两个依赖类之后将此关系()添加到类中。

似乎用户的关系组无法识别UserGroup,当两个类继承相同的基类时,脚本是正确的。

我想问一下这是什么问题?

0 个答案:

没有答案