Warning about using backref with viewonly - how to make a two-way read-only relation?

214 views
Skip to first unread message

Tony Hignett

unread,
Mar 18, 2020, 11:22:23 AM3/18/20
to sqlalchemy
Hi,

I'm trying to upgrade from 1.2.2 to 1.3.15 and a number of our relations have started generating these warnings:
```
SAWarning: Setting backref / back_populates on relationship <child.parent> to refer to viewonly relationship <parent.children> will be deprecated in SQLAlchemy 1.4, and will be disallowed in a future release.  viewonly relationships should not be mutated (this warning may be suppressed after 10 occurrences)
  (self, other),
```

I've read some of the related issues, e.g. https://github.com/sqlalchemy/sqlalchemy/issues/5149https://github.com/sqlalchemy/sqlalchemy/issues/4993, but I don't understand how backref comes into it. We don't want to mutate anything along the backref.

Is there a way of expressing a two-way read-only relation?

The code looks like
```
class Parent(Base):
    pass

class Child1(Base):
    
    @declared_attr
    def parent(cls):
        return orm.relationship(
            Parent,
            ...,
            viewonly=True,
            backref="children1"
        )

class Child2(Base):
    [etc]
````

Mike Bayer

unread,
Mar 18, 2020, 11:42:38 AM3/18/20
to noreply-spamdigest via sqlalchemy


On Wed, Mar 18, 2020, at 11:22 AM, Tony Hignett wrote:
Hi,

I'm trying to upgrade from 1.2.2 to 1.3.15 and a number of our relations have started generating these warnings:
```
SAWarning: Setting backref / back_populates on relationship <child.parent> to refer to viewonly relationship <parent.children> will be deprecated in SQLAlchemy 1.4, and will be disallowed in a future release.  viewonly relationships should not be mutated (this warning may be suppressed after 10 occurrences)
  (self, other),
```

I've read some of the related issues, e.g. https://github.com/sqlalchemy/sqlalchemy/issues/5149https://github.com/sqlalchemy/sqlalchemy/issues/4993, but I don't understand how backref comes into it. We don't want to mutate anything along the backref.

Is there a way of expressing a two-way read-only relation?

you should remove the backref.   have the "viewonly" relationship stand alone by itself.



The code looks like
```
class Parent(Base):
    pass

class Child1(Base):
    
    @declared_attr
    def parent(cls):
        return orm.relationship(
            Parent,
            ...,
            viewonly=True,
            backref="children1"
        )

class Child2(Base):
    [etc]
````


--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
 
 
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+...@googlegroups.com.

Mike Bayer

unread,
Mar 18, 2020, 11:43:30 AM3/18/20
to noreply-spamdigest via sqlalchemy


On Wed, Mar 18, 2020, at 11:42 AM, Mike Bayer wrote:


On Wed, Mar 18, 2020, at 11:22 AM, Tony Hignett wrote:
Hi,

I'm trying to upgrade from 1.2.2 to 1.3.15 and a number of our relations have started generating these warnings:
```
SAWarning: Setting backref / back_populates on relationship <child.parent> to refer to viewonly relationship <parent.children> will be deprecated in SQLAlchemy 1.4, and will be disallowed in a future release.  viewonly relationships should not be mutated (this warning may be suppressed after 10 occurrences)
  (self, other),
```

I've read some of the related issues, e.g. https://github.com/sqlalchemy/sqlalchemy/issues/5149https://github.com/sqlalchemy/sqlalchemy/issues/4993, but I don't understand how backref comes into it. We don't want to mutate anything along the backref.

Is there a way of expressing a two-way read-only relation?

you should remove the backref.   have the "viewonly" relationship stand alone by itself.

also if you want 'children1' to also be a viewonly relationship, set that up separately.


Jonathan Vanasco

unread,
Mar 18, 2020, 12:11:48 PM3/18/20
to sqlalchemy
Tony,

Mike helped me with a similar problem a few weeks ago:

https://groups.google.com/d/msg/sqlalchemy/k4a-v2ebeJM/bj73xd4CFwAJ

In his suggestion, I stash some mapper configuration data into `info`, then use the `mapper_configured` event to audit my configuration requirements. 
Reply all
Reply to author
Forward
0 new messages