我正在尝试定义用户的多对多关系。我跟着文档,我得到的最好的是:
import sqlalchemy as sa
from sqlalchemy import orm
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class Friends(Base):
__tablename__ = 'friends'
user1_id = sa.Column(sa.ForeignKey('users.id'), primary_key=True)
user2_id = sa.Column(sa.ForeignKey('users.id'), primary_key=True)
class User(Base):
__tablename__ = 'users'
id = sa.Column(sa.Integer, primary_key=True)
name = sa.Column(sa.String(50))
# many to many
friends = orm.relationship('User', secondary=Friends, backref=Friends)
u = User(name='someuser')
但这会给我
“AttributeError: 'Friends' 对象没有属性 'foreign_keys”
怎么了?