如何在sqlalchemy中通过关联代理向关系中添加实例?

2022-09-28 20:51:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下代码:

class Request(BaseMixin, Base):
    id = Column(Integer, primary_key=True)
    title = Column(String(30))
    servers = association_proxy('request_servers','server')


class Server(BaseMixin, Base):
    ip = Column(String(30), primary_key=True, autoincrement=False)
    status = Column(String(15))


class Request2Server(AssocBaseMixin, Base):
    request_id_fk = Column(Integer, ForeignKey('request.id'), primary_key=True)
    server_ip_fk = Column(String(30), ForeignKey('server.ip'), primary_key=True)
    request = relationship('Request', backref=backref('request_servers'))
    server = relationship('Server')

注意-mixin只是设置tablename

当我执行时:

request1 = Request(scan_title='tttt',ip='10.0.1.1')
server1 = Server(ip='30.0.1.3',server_id_fk=9)
request1.servers.append(server1)

它产生:

TypeError: init() takes 1 positional argument but 2 were given

有什么建议可以解决这个问题吗

谢谢 雅诺


Tags: keyipidtruebasestringserverrequestcolumnclassfkserversprimary

热门问题