Hi all, I need some clarification on the following RemovedIn20Warning
"Pet" object is being merged into a Session along the backref cascade path for relationship "Person.pets"; in SQLAlchemy 2.0, this reverse cascade will not take place... Does it really means that in SA 2.0 the pattern new_entity = Entity(attached_to_object_id=1) won't work at all, or instead that the "other end" one-to-many relationship list won't be automatically updated? If the latter, is there any way to say "ok, I don't care about the reverse cascade updates in this particular case, as no further accesses to the one-to-many rels on the other side will happen"? Some background on my usage: the app I'm upgrading to SA 1.4 is based on Pyramid and has a generic way to build CRUD pages for a given entity, so visiting, say /Person/{person_id}/Pet/add will present a form (automatically generated by deform) able to create a new Pet entity linked to the given Person; its generic POST handler determines the target entity from the request's query string and will basically do # obtain "owner" ID from the request owners = {key: value for key in request.matchdict if key.endswith('_id')} pet = Pet(**owners) session.add(pet) # apply form's data pet.edit(request.POST.items()) session.commit() This triggers the mentioned SA 1.4 warning when executed with SQLALCHEMY_WARN_20=1, as the attached snippet exhibits. I tried several variants, such as using "back_populates" instead of the "old" backref, but the only pattern that removed the warning has been owner = session.get(Person, request.matchdict['person_id']) pet = Pet() owner.pets.append(pet) where I surely can get, if needed, but would require implementing more per-entity specific knowledge (possibly using introspection) into the generic CRUD machinery, to "convert" from the "person_id" to the name of the "other side" relationship attribute, in several dozens of places. Another approach could be configuring all the "one-to-many" relationships as "viewonly", but that again would require more analysis on the code paths... So, am I missing something and there is some recommended alternative, or should I dig into the rabbit's hole? Thanks in advance for any hint, ciao, lele. -- 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 sqlalchemy+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/87wnpg59dm.fsf%40metapensiero.it.
from sqlalchemy import (Column, ForeignKey, Integer, MetaData, String, Text, Table, create_engine, orm) metadata = MetaData() Base = orm.declarative_base(metadata=metadata) class Person(Base): __tablename__ = 'persons' id = Column(Integer, primary_key=True) firstname = Column(String) class Pet(Base): __tablename__ = 'pets' id = Column(Integer, primary_key=True) name = Column(String) person_id = Column(Integer, ForeignKey('persons.id')) person = orm.relationship(Person, backref=orm.backref('pets')) engine = create_engine('sqlite:///:memory:', echo=True) session = orm.sessionmaker(bind=engine)() metadata.create_all(engine) me = Person(firstname='Lele') session.add(me) yaku = Pet(person=me, name='Yaku') session.add(yaku) session.commit()
-- nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia. l...@metapensiero.it | -- Fortunato Depero, 1929. -- 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 sqlalchemy+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/87wnpg59dm.fsf%40metapensiero.it.