不能確定你的三個表的名稱是什麼,但是用這個作爲一個例子:
SQL:
create table users (
user_id bigint unsigned not null auto_increment,
username varchar(50),
email varchar(250),
primary key (user_id)
);
create table users_info (
user_id bigint unsigned not null,
status tinyint(1) unsigned,
date_added bigint(10) unsigned,
primary key (user_id)
);
Python:
many_to_many = Table(
'many_to_many', Base.metadata,
Column('many_left_id', BigInteger,
ForeignKey('users.user_id'), primary_key=True),
Column('many_right_id', BigInteger,
ForeignKey('users_info.user_id'), primary_key=True)
)
class users(Base):
__tablename__ = 'users'
user_id = Column(BigInteger, primary_key=True)
username = Column(String)
email = Column(String)
many_right = relationship(
'users',
secondary=many_to_many,
primaryjoin=user_id==many_to_many.c.many_left_id,
secondaryjoin=user_id==many_to_many.c.many_right_id,
backref='many_left'
)
class users_info(Base):
__tablename__ = 'users_info'
user_id = Column(BigInteger, primary_key=True)
status = Column(Boolean)
date_added = Column(BigInteger)
many_right = relationship(
'users_info',
secondary=many_to_many,
primaryjoin=user_id==many_to_many.c.many_left_id,
secondaryjoin=user_id==many_to_many.c.many_right_id,
backref='many_left'
)
只是爲了澄清:是'(ITEM_ID,USER_ID)'對在此表中是唯一? 「狀態」表有哪些字段? – van 2012-01-09 21:58:14