Here's an MCVE. Works fine as long as "Abstract.is_deleted" is in fact
False. if it's true, then it won't mark it, because it's not in the
collection to be updated.
from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class ContributionType(Base):
__tablename__ = 'contributiontype'
id = Column(Integer, primary_key=True)
class Abstract(Base):
__tablename__ = 'abstract'
id = Column(Integer, primary_key=True)
is_deleted = Column(Boolean)
contrib_type_id = Column(ForeignKey('contributiontype.id'))
contrib_type = relationship(
'ContributionType', lazy=True, foreign_keys=contrib_type_id,
backref=backref(
'proposed_abstracts',
primaryjoin='(Abstract.contrib_type_id ==
ContributionType.id) & (~Abstract.is_deleted)',
lazy=True)
)
e = create_engine("sqlite://", echo=True)
Base.metadata.create_all(e)
s = Session(e)
cont = ContributionType()
abst = Abstract(contrib_type=cont, is_deleted=0) # if you set this to
'1', it doesn't get deleted
s.add_all([cont, abst])
s.commit()
s.delete(cont)
s.commit()
assert abst.contrib_type_id is None
On 04/19/2017 09:26 AM, mike bayer wrote:
On 04/19/2017 07:27 AM, Adrian wrote:
I have this relationship which adds a
`ContributionType.proposed_abstracts` backref that contains only
abstracts not flagged as deleted.
contrib_type = relationship(
'ContributionType', lazy=True, foreign_keys=contrib_type_id,
backref=backref('proposed_abstracts',
primaryjoin='(Abstract.contrib_type_id == ContributionType.id) &
~Abstract.is_deleted', lazy=True)
)
This works perfectly fine but unfortunately a
`session.delete(some_contribution_type)` now does not NULL out the
contrib_type_id of an abstract that
has been flagged as deleted.
can you clarify what "now" means? what's the version that "worked" ?
Is there any way to use different join criteria for deletion cascades
and for just accessing the relationship? Or do I need to hook into the
before_delete
event for this?
--
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]
<mailto:[email protected]>.
To post to this group, send email to [email protected]
<mailto:[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.