class User(Base):
"""
users table
"""
__tablename__ = 'users'
__table_args__ = {
'mysql_engine': 'InnoDB',
'mysql_charset': 'utf8'
}
id = Column(INTEGER, primary_key=True)
email = Column(String(64), nullable=False, unique=True)
password = Column(String(64), nullable=False)
name = Column(String(32), nullable=False)
last_login_time = Column(DateTime, default='')
last_login_ip = Column(String(32), default='')
create_time = Column(DateTime, nullable=False)
status = Column(SMALLINT, default=0)
role = relationship("Role",
secondary="role_user",
backref=backref('user'))
def __init__(self, email, password, name, last_login_time=now,
last_login_ip='0', create_time=now, status=0):
self.email = email
self.password = md5(password)
self.name = name
self.last_login_time = last_login_time
self.last_login_ip = last_login_ip
self.create_time = create_time
self.status = status
def __repr__(self):
return "<User('%s', '%s')>" % (self.name, self.email)
class Role(Base):
"""
roles table
"""
__tablename__ = 'roles'
__table_args__ = {
'mysql_engine': 'InnoDB',
'mysql_charset': 'utf8'
}
id = Column(SMALLINT(5), primary_key=True)
name = Column(String(32), nullable=False, unique=True)
def __init__(self, name):
self.name = name
def __repr__(self):
return "<Role('%s', '%s')>" % (self.id, self.name)
class role_user(Base):
"""
role_user association table
"""
__tablename__ = 'role_user'
__table_args__ = {
'mysql_engine': 'InnoDB',
'mysql_charset': 'utf8'
}
user_id = Column(INTEGER, ForeignKey('users.id'), primary_key=True)
role_id = Column(SMALLINT(5), ForeignKey('roles.id'), primary_key=True)
def __init__(self, user_id, role_id):
self.user_id = user_id
self.role_id = role_id
ユーザーを作成すると、次のエラーが発生します。
u1 = User(email='john@example.com', password='12345', name='john')
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "<string>", line 2, in __init__
File "/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.8.1dev-py2.6-linux-i686.egg/sqlalchemy/orm/instrumentation.py", line 310, in _new_state_if_none
state = self._state_constructor(instance, self)
File "/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.8.1dev-py2.6-linux-i686.egg/sqlalchemy/util/langhelpers.py", line 582, in __get__
obj.__dict__[self.__name__] = result = self.fget(obj)
File "/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.8.1dev-py2.6-linux-i686.egg/sqlalchemy/orm/instrumentation.py", line 145, in _state_constructor
self.dispatch.first_init(self, self.class_)
File "/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.8.1dev-py2.6-linux-i686.egg/sqlalchemy/event.py", line 409, in __call__
fn(*args, **kw)
File "/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.8.1dev-py2.6-linux-i686.egg/sqlalchemy/orm/mapper.py", line 2209, in _event_on_first_init
configure_mappers()
File "/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.8.1dev-py2.6-linux-i686.egg/sqlalchemy/orm/mapper.py", line 2118, in configure_mappers
mapper._post_configure_properties()
File "/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.8.1dev-py2.6-linux-i686.egg/sqlalchemy/orm/mapper.py", line 1242, in _post_configure_properties
prop.init()
File "/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.8.1dev-py2.6-linux-i686.egg/sqlalchemy/orm/interfaces.py", line 231, in init
self.do_init()
File "/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.8.1dev-py2.6-linux-i686.egg/sqlalchemy/orm/properties.py", line 1028, in do_init
self._setup_join_conditions()
File "/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.8.1dev-py2.6-linux-i686.egg/sqlalchemy/orm/properties.py", line 1102, in _setup_join_conditions
can_be_synced_fn=self._columns_are_mapped
File "/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.8.1dev-py2.6-linux-i686.egg/sqlalchemy/orm/relationships.py", line 114, in __init__
self._determine_joins()
File "/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.8.1dev-py2.6-linux-i686.egg/sqlalchemy/orm/relationships.py", line 180, in _determine_joins
consider_as_foreign_keys=consider_as_foreign_keys
File "/usr/local/lib/python2.6/dist-packages/SQLAlchemy-0.8.1dev-py2.6-linux-i686.egg/sqlalchemy/sql/util.py", line 345, in join_condition
b.foreign_keys,
AttributeError: type object 'role_user' has no attribute 'foreign_keys'
>>>
jiankong@ubuntu:~/git/admin-server$ python manage.py shell
Python 2.6.5 (r265:79063, Oct 1 2012, 22:07:21)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from apps.auth.models import User
>>> u1 = User(email='john@example.com', password='12345', name='john')
質問を解決するにはどうすればよいですか? ありがとうございました !!