Python SQLAlchemy:\uuuu init\uuuu()接受1个位置参数,但给出了2个(多对多)

Python SQLAlchemy:\uuuu init\uuuu()接受1个位置参数,但给出了2个(多对多),python,sqlite,sqlalchemy,flask-sqlalchemy,Python,Sqlite,Sqlalchemy,Flask Sqlalchemy,SQLAlchemy无疑是非常强大的,但是文档隐式地假设了大量关于关系的先验知识,混合了backref和新首选的back\u populates()方法,这让我感到非常困惑 下面的模型设计与文档中的指南非常相似,文档中涉及了。您可以看到,注释仍然与原始文章中的注释相同,我只更改了实际代码 class MatchTeams(db.Model): match_id = db.Column(db.String, db.ForeignKey('match.id'), primary_key=Tr

SQLAlchemy无疑是非常强大的,但是文档隐式地假设了大量关于关系的先验知识,混合了
backref
和新首选的
back\u populates()
方法,这让我感到非常困惑

下面的模型设计与文档中的指南非常相似,文档中涉及了。您可以看到,注释仍然与原始文章中的注释相同,我只更改了实际代码

class MatchTeams(db.Model):
    match_id = db.Column(db.String, db.ForeignKey('match.id'), primary_key=True)
    team_id = db.Column(db.String, db.ForeignKey('team.id'), primary_key=True)
    team_score = db.Column(db.Integer, nullable="True")

    # bidirectional attribute/collection of "user"/"user_keywords"
    match = db.relationship("Match",
                            backref=db.backref("match_teams",
                                            cascade="all, delete-orphan")
                            )
    # reference to the "Keyword" object
    team = db.relationship("Team")


class Match(db.Model):
    id = db.Column(db.String, primary_key=True)

    # Many side of many to one with Round
    round_id = db.Column(db.Integer, ForeignKey('round.id'))
    round = db.relationship("Round", back_populates="matches")
    # Start of M2M

    # association proxy of "match_teams" collection
    # to "team" attribute
    teams = association_proxy('match_teams', 'team')

    def __repr__(self):
        return '<Match: %r>' % (self.id)


class Team(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String, nullable=False)
    goals_for = db.Column(db.Integer)
    goals_against = db.Column(db.Integer)
    wins = db.Column(db.Integer)
    losses = db.Column(db.Integer)
    points = db.Column(db.Integer)
    matches_played = db.Column(db.Integer)

    def __repr__(self):
        return '<Team %r with ID: %r>' % (self.name, self.id)
并输出如下:

Traceback (most recent call last):
File "/REDACT/temp.py", line 12, in <module>
find_match.teams.append(find_liverpool)
File "/REDACT/lib/python3.4/site-packages/sqlalchemy/ext/associationproxy.py", line 609, in append
item = self._create(value)
File "/REDACT/lib/python3.4/site-packages/sqlalchemy/ext/associationproxy.py", line 532, in _create
 return self.creator(value)
TypeError: __init__() takes 1 positional argument but 2 were given    
<Team 'Liverpool' with ID: 1>
<Match: '123'>
回溯(最近一次呼叫最后一次):
文件“/REDACT/temp.py”,第12行,在
查找比赛。球队。追加(查找利物浦)
文件“/REDACT/lib/python3.4/site packages/sqlalchemy/ext/associationproxy.py”,第609行,在append中
项目=自我创造(价值)
文件“/REDACT/lib/python3.4/site packages/sqlalchemy/ext/associationproxy.py”,第532行,在创建
返回self.creator(值)
TypeError:\uuuu init\uuuuuu()接受1个位置参数,但提供了2个
从文档中可以看出,对的调用正在尝试调用
MatchTeam
。这在链接到的“简化关联对象”下也有说明:

其中,每个
.keywords.append()
操作相当于:

>>user.user\u keywords.append(UserKeyword(关键字('its\u heavy'))

因此你

find_match.teams.append(find_liverpool)
相当于

find_match.match_teams.append(MatchTeams(find_liverpool))
由于
MatchTeams
没有明确定义的
\uuuuu init\uuuuu
,因此它使用as(除非您已重写它),它除了唯一的位置参数
self
之外,只接受关键字参数

要解决此问题,请将工厂传递给您的关联代理:

class Match(db.Model):

    teams = association_proxy('match_teams', 'team',
                              creator=lambda team: MatchTeams(team=team))
或者在
MatchTeams
上定义
\uuuu init\uuuu
以满足您的需要,例如:

class MatchTeams(db.Model):

    # Accepts as positional arguments as well
    def __init__(self, team=None, match=None):
        self.team = team
        self.match = match
或显式创建关联对象:

db.session.add(MatchTeams(match=find_match, team=find_liverpool))
# etc.

你需要提供更多的信息。是什么导致了这种错误?它不可能是您发布的代码段,因为如果第一行出现错误,则不会填充
find\u liverpool
,但显然是这样。请显示完整的代码并进行回溯。@DanielRoseman我发布的代码就是产生错误的原因。我添加了完整的回溯。find_liverpool在
TypeError
之前填充,但出于某种原因,打印输出在它之后。
db.session.add(MatchTeams(match=find_match, team=find_liverpool))
# etc.