oh nevermind, this is a simple issue:
patches = association_proxy(
"patches", "patch", creator=lambda p: Server2Patch(patch=p)
)
conflicts with:
request_server = relationship(
"Request2Server",
backref=backref("patches"),
foreign_keys=[
request2server_request_id_fk,
request2server_server_ip_fk,
],
)
it is overwriting the "patches" relationship. Change to:
patches = association_proxy(
"_patches", "patch", creator=lambda p: Server2Patch(patch=p)
)
request_server = relationship(
"Request2Server",
backref=backref("_patches"),
foreign_keys=[
request2server_request_id_fk,
request2server_server_ip_fk,
],
)
the association proxy should raise for this condition, e.g. pointing
it to itself.
On Thu, Apr 25, 2019 at 11:00 AM Mike Bayer <[email protected]> wrote:
>
> On Thu, Apr 25, 2019 at 8:34 AM Jan Sakalos <[email protected]> wrote:
> >
> > Hello,
> >
> > I have to association proxies in code one is working and other not. I went
> > through it many times and wasnt able to identify issue.
> > Also please can you give me advice how to debug such issues if there is any?
>
> The error is that a backref is not being located, which normally has
> nothing to do with the association proxies. However it's not clear
> if the association proxies are getting in the way of the
> relationships.
>
> The first step would be to run your operation without using the
> association proxies, that is, create the Server2Patch object manually
> and append it as though the association proxies weren't there.
>
> Unfortunately I can't visually see how the error would occur which
> means it is quite possible that some very unlikely interaction is
> going on that can't be easily guessed, so I'll have to run it.
>
> >
> > Thanks
> > Jano
> >
> > code:
> >
> >
> > from sqlalchemy import Column, Integer, String, ForeignKey, DateTime, Time,
> > ForeignKeyConstraint, and_, create_engine
> > from sqlalchemy.ext.associationproxy import association_proxy
> > from sqlalchemy.ext.declarative import declared_attr, declarative_base
> > from sqlalchemy.orm import relationship, backref, sessionmaker
> >
> > engine_qualys =
> > create_engine('mysql+pymysql://qualys:123456@localhost/sqlalchemy_test')
> > Base = declarative_base()
> > Base.metadata.create_all(engine_qualys)
> >
> >
> > Session = sessionmaker(bind=engine_qualys)
> >
> >
> > class BaseMixin():
> >
> > @declared_attr
> > def __tablename__(cls):
> > return cls.__name__.lower()
> >
> > def __repr__(self):
> > values = ', '.join("%s=%r" % (n, getattr(self, n)) for n in
> > self.__table__.c.keys())
> > return "%s(%s)" % (self.__class__.__name__, values)
> >
> >
> > class Request(BaseMixin, Base):
> > id = Column(Integer, primary_key=True)
> > ip = Column(String(30))
> >
> > #working
> > servers = association_proxy('request_servers', 'server', creator=lambda
> > server: Request2Server(server=server))
> >
> >
> > class Server(BaseMixin, Base):
> > ip = Column(String(30), primary_key=True, autoincrement=False)
> >
> >
> > class Request2Server(BaseMixin, Base):
> > request_id_fk = Column(Integer, ForeignKey('request.id'),
> > primary_key=True)
> > server_ip_fk = Column(String(30), ForeignKey('server.ip'),
> > primary_key=True)
> > data = Column(String(30))
> >
> > request = relationship('Request', backref=backref('request_servers'))
> > server = relationship('Server', backref=backref('server_requests'))
> >
> > #not working
> > patches = association_proxy('patches', 'patch', creator=lambda p:
> > Server2Patch(patch=p))
> >
> >
> > class Patch(BaseMixin, Base):
> > qid = Column(Integer, primary_key=True)
> >
> >
> > class Server2Patch(BaseMixin, Base):
> > request2server_request_id_fk = Column(Integer,
> > ForeignKey('request2server.request_id_fk'), primary_key=True)
> > request2server_server_ip_fk = Column(String(30),
> > ForeignKey('request2server.server_ip_fk'), primary_key=True)
> > patch_qid_fk = Column(Integer, ForeignKey('patch.qid'),
> > primary_key=True)
> >
> > request_server = relationship('Request2Server',
> > backref=backref('patches'), foreign_keys=[request2server_request_id_fk,
> > request2server_server_ip_fk])
> > patch = relationship('Patch', backref=backref('request_servers2patch'),
> > foreign_keys=[patch_qid_fk])
> >
> > __table_args__ = (ForeignKeyConstraint([request2server_request_id_fk,
> > request2server_server_ip_fk],
> > [Request2Server.request_id_fk,
> > Request2Server.server_ip_fk]),
> > {})
> >
> >
> > Base.metadata.create_all(engine_qualys)
> >
> > s = Session()
> >
> > #generating some data
> > request1 = Request(scan_title='tttt', ip='1.1.1.1,2.2.2.2')
> > server1 = Server(ip='1.1.1.1')
> > server2 = Server(ip='2.2.2.2')
> > request1.servers.append(server1)
> > request1.servers.append(server2)
> > request1_server1 = request1.request_servers[0]
> > patch1 = Patch(qid=1)
> > s.add_all([request1, server1, server2, patch1])
> > s.commit()
> >
> > #this is not working
> > request1_server1.patches.append(patch1):
> >
> > Traceback (most recent call last):
> > File "<input>", line 1, in <module>
> > File
> > "/home/saki/PycharmProjects/sqlalchemy_test/venv/lib64/python3.7/site-packages/sqlalchemy/orm/collections.py",
> > line 1113, in append
> > item = __set(self, item, _sa_initiator)
> > File
> > "/home/saki/PycharmProjects/sqlalchemy_test/venv/lib64/python3.7/site-packages/sqlalchemy/orm/collections.py",
> > line 1078, in __set
> > item = executor.fire_append_event(item, _sa_initiator)
> > File
> > "/home/saki/PycharmProjects/sqlalchemy_test/venv/lib64/python3.7/site-packages/sqlalchemy/orm/collections.py",
> > line 715, in fire_append_event
> > self.owner_state, self.owner_state.dict, item, initiator
> > File
> > "/home/saki/PycharmProjects/sqlalchemy_test/venv/lib64/python3.7/site-packages/sqlalchemy/orm/attributes.py",
> > line 1134, in fire_append_event
> > value = fn(state, value, initiator or self._append_token)
> > File
> > "/home/saki/PycharmProjects/sqlalchemy_test/venv/lib64/python3.7/site-packages/sqlalchemy/orm/attributes.py",
> > line 1444, in emit_backref_from_collection_append_event
> > child_impl = child_state.manager[key].impl
> > KeyError: 'request_server'
> >
> >
> > --
> > SQLAlchemy -
> > The Python SQL Toolkit and Object Relational Mapper
> >
> > http://www.sqlalchemy.org/
> >
> > To post example code, please provide an MCVE: Minimal, Complete, and
> > Verifiable Example. See http://stackoverflow.com/help/mcve for a full
> > description.
> > ---
> > You received this message because you are subscribed to the Google Groups
> > "sqlalchemy" group.
> > To unsubscribe from this group and stop receiving emails from it, send an
> > email to [email protected].
> > To post to this group, send email to [email protected].
> > Visit this group at https://groups.google.com/group/sqlalchemy.
> > For more options, visit https://groups.google.com/d/optout.
--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
http://www.sqlalchemy.org/
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable
Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.