Re: [sqlalchemy] Deletion of object with relationship(lazy='raise')

2019-11-22 Thread Mike Bayer
I can confirm this is a bug as I've found precedent for this not having to 
raise, new issue is
https://github.com/sqlalchemy/sqlalchemy/issues/4997



On Fri, Nov 22, 2019, at 9:24 AM, Mike Bayer wrote:
> that said, I'm looking at this as a potential bug because it should be able 
> to leave this attribute alone, not really sure how this should be handled.
> 
> On Fri, Nov 22, 2019, at 9:21 AM, Mike Bayer wrote:
>> lazy="raise" on a many to one is problematic because a lot of many-to-one 
>> operations involve pulling up the object from the identity map, and that's 
>> it. since people are usually only trying to guard against SQL being emitted, 
>> use the raise_on_sql option instead:
>> 
>> parent = relationship(Parent, lazy='raise_on_sql')
>> 
>> thanks for the perfect test case BTW makes this very easy for me
>> 
>> On Fri, Nov 22, 2019, at 6:12 AM, Marat Sharafutdinov wrote:
>>> from sqlalchemy import Column, ForeignKey, Integer, create_engine
>>> from sqlalchemy.ext.declarative import declarative_base
>>> from sqlalchemy.orm import relationship, sessionmaker
>>> 
>>> Base = declarative_base()
>>> 
>>> class Parent(Base):
>>>  __tablename__ = 'parents'
>>>  id = Column(Integer, primary_key=True)
>>> 
>>> class Child(Base):
>>>  __tablename__ = 'children'
>>>  id = Column(Integer, primary_key=True)
>>>  parent_id = Column(Integer, ForeignKey(Parent.id))
>>>  parent = relationship(Parent, lazy='raise', passive_deletes=True)
>>> 
>>> engine = create_engine('sqlite:///:memory:', echo=True)
>>> Base.metadata.create_all(engine)
>>> Session = sessionmaker(bind=engine)
>>> session = Session()
>>> 
>>> # Add
>>> parent = Parent()
>>> session.add(parent)
>>> session.flush()
>>> child = Child(parent_id=parent.id)
>>> session.add(child)
>>> session.commit()
>>> 
>>> # Delete
>>> child = session.query(Child).first()
>>> session.delete(child)
>>> session.commit()
>>> 
>>> Now I'm getting the following warning:
>>> /.venv/lib/python3.8/site-packages/sqlalchemy/orm/relationships.py:2021: 
>>> SAWarning: On Child.parent, 'passive_deletes' is normally configured on 
>>> one-to-many, one-to-one, many-to-many relationships only.
>>> 
>>> If I change `parent` relationship as follows:
>>> parent = relationship(Parent, lazy='raise', backref=backref('children', 
>>> passive_deletes=True))
>>> then I would get the following exception:
>>> sqlalchemy.exc.InvalidRequestError: 'Child.parent' is not available due to 
>>> lazy='raise'
>>> 
>>> How should it be made properly?
>>> 

>>> --
>>> 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/40f63feb-383e-4fee-8db6-21a757887c54%40googlegroups.com
>>>  
>>> .
>> 
>> 

>> --
>> 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/79e8c08e-c761-4d48-96d8-4cabf6de36de%40www.fastmail.com
>>  
>> .
> 
> 

> --
>  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/e99fe1a3-0547-4dae-ab61-e1cd26ccd879%40www.fastmail.com
>  
> .

-- 
SQLAlchemy - 
The Python 

Re: [sqlalchemy] Deletion of object with relationship(lazy='raise')

2019-11-22 Thread Mike Bayer
that said, I'm looking at this as a potential bug because it should be able to 
leave this attribute alone, not really sure how this should be handled.

On Fri, Nov 22, 2019, at 9:21 AM, Mike Bayer wrote:
> lazy="raise" on a many to one is problematic because a lot of many-to-one 
> operations involve pulling up the object from the identity map, and that's 
> it. since people are usually only trying to guard against SQL being emitted, 
> use the raise_on_sql option instead:
> 
> parent = relationship(Parent, lazy='raise_on_sql')
> 
> thanks for the perfect test case BTW makes this very easy for me
> 
> On Fri, Nov 22, 2019, at 6:12 AM, Marat Sharafutdinov wrote:
>> from sqlalchemy import Column, ForeignKey, Integer, create_engine
>> from sqlalchemy.ext.declarative import declarative_base
>> from sqlalchemy.orm import relationship, sessionmaker
>> 
>> Base = declarative_base()
>> 
>> class Parent(Base):
>>  __tablename__ = 'parents'
>>  id = Column(Integer, primary_key=True)
>> 
>> class Child(Base):
>>  __tablename__ = 'children'
>>  id = Column(Integer, primary_key=True)
>>  parent_id = Column(Integer, ForeignKey(Parent.id))
>>  parent = relationship(Parent, lazy='raise', passive_deletes=True)
>> 
>> engine = create_engine('sqlite:///:memory:', echo=True)
>> Base.metadata.create_all(engine)
>> Session = sessionmaker(bind=engine)
>> session = Session()
>> 
>> # Add
>> parent = Parent()
>> session.add(parent)
>> session.flush()
>> child = Child(parent_id=parent.id)
>> session.add(child)
>> session.commit()
>> 
>> # Delete
>> child = session.query(Child).first()
>> session.delete(child)
>> session.commit()
>> 
>> Now I'm getting the following warning:
>> /.venv/lib/python3.8/site-packages/sqlalchemy/orm/relationships.py:2021: 
>> SAWarning: On Child.parent, 'passive_deletes' is normally configured on 
>> one-to-many, one-to-one, many-to-many relationships only.
>> 
>> If I change `parent` relationship as follows:
>> parent = relationship(Parent, lazy='raise', backref=backref('children', 
>> passive_deletes=True))
>> then I would get the following exception:
>> sqlalchemy.exc.InvalidRequestError: 'Child.parent' is not available due to 
>> lazy='raise'
>> 
>> How should it be made properly?
>> 

>> --
>> 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/40f63feb-383e-4fee-8db6-21a757887c54%40googlegroups.com
>>  
>> .
> 
> 

> --
>  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/79e8c08e-c761-4d48-96d8-4cabf6de36de%40www.fastmail.com
>  
> .

-- 
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/e99fe1a3-0547-4dae-ab61-e1cd26ccd879%40www.fastmail.com.


Re: [sqlalchemy] Deletion of object with relationship(lazy='raise')

2019-11-22 Thread Mike Bayer
lazy="raise" on a many to one is problematic because a lot of many-to-one 
operations involve pulling up the object from the identity map, and that's it. 
since people are usually only trying to guard against SQL being emitted, use 
the raise_on_sql option instead:

parent = relationship(Parent, lazy='raise_on_sql')

thanks for the perfect test case BTW makes this very easy for me

On Fri, Nov 22, 2019, at 6:12 AM, Marat Sharafutdinov wrote:
> from sqlalchemy import Column, ForeignKey, Integer, create_engine
> from sqlalchemy.ext.declarative import declarative_base
> from sqlalchemy.orm import relationship, sessionmaker
> 
> Base = declarative_base()
> 
> class Parent(Base):
>  __tablename__ = 'parents'
>  id = Column(Integer, primary_key=True)
> 
> class Child(Base):
>  __tablename__ = 'children'
>  id = Column(Integer, primary_key=True)
>  parent_id = Column(Integer, ForeignKey(Parent.id))
>  parent = relationship(Parent, lazy='raise', passive_deletes=True)
> 
> engine = create_engine('sqlite:///:memory:', echo=True)
> Base.metadata.create_all(engine)
> Session = sessionmaker(bind=engine)
> session = Session()
> 
> # Add
> parent = Parent()
> session.add(parent)
> session.flush()
> child = Child(parent_id=parent.id)
> session.add(child)
> session.commit()
> 
> # Delete
> child = session.query(Child).first()
> session.delete(child)
> session.commit()
> 
> Now I'm getting the following warning:
> /.venv/lib/python3.8/site-packages/sqlalchemy/orm/relationships.py:2021: 
> SAWarning: On Child.parent, 'passive_deletes' is normally configured on 
> one-to-many, one-to-one, many-to-many relationships only.
> 
> If I change `parent` relationship as follows:
> parent = relationship(Parent, lazy='raise', backref=backref('children', 
> passive_deletes=True))
> then I would get the following exception:
> sqlalchemy.exc.InvalidRequestError: 'Child.parent' is not available due to 
> lazy='raise'
> 
> How should it be made properly?
> 

> --
>  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/40f63feb-383e-4fee-8db6-21a757887c54%40googlegroups.com
>  
> .

-- 
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/79e8c08e-c761-4d48-96d8-4cabf6de36de%40www.fastmail.com.


[sqlalchemy] Deletion of object with relationship(lazy='raise')

2019-11-22 Thread Marat Sharafutdinov
from sqlalchemy import Column, ForeignKey, Integer, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, sessionmaker

Base = declarative_base()

class Parent(Base):
__tablename__ = 'parents'
id = Column(Integer, primary_key=True)

class Child(Base):
__tablename__ = 'children'
id = Column(Integer, primary_key=True)
parent_id = Column(Integer, ForeignKey(Parent.id))
parent = relationship(Parent, lazy='raise', passive_deletes=True)

engine = create_engine('sqlite:///:memory:', echo=True)
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()

# Add
parent = Parent()
session.add(parent)
session.flush()
child = Child(parent_id=parent.id)
session.add(child)
session.commit()

# Delete
child = session.query(Child).first()
session.delete(child)
session.commit()

Now I'm getting the following warning:
/.venv/lib/python3.8/site-packages/sqlalchemy/orm/relationships.py:2021: 
SAWarning: On Child.parent, 'passive_deletes' is normally configured on one-
to-many, one-to-one, many-to-many relationships only.

If I change `parent` relationship as follows:
parent = relationship(Parent, lazy='raise', backref=backref('children', 
passive_deletes=True))
then I would get the following exception:
sqlalchemy.exc.InvalidRequestError: 'Child.parent' is not available due to 
lazy='raise'

How should it be made properly?

-- 
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/40f63feb-383e-4fee-8db6-21a757887c54%40googlegroups.com.


[sqlalchemy] Assert contents of DB tables

2019-11-22 Thread sumau
Hello

I would like to assert the contents of tables in my PG schema i.e. make 
sure it contains the data I'm expecting

I am aware of various options:

1) Compare the actual and expected tables using a sql query, orchestrated 
by sqlalchemy (i.e. create the actual and expected tables in DB, run the 
sql comparison script, return the output)
2) Load the actual tables as tuples and compare them with expected tuples 
using something like assert_result
https://github.com/sqlalchemy/sqlalchemy/blob/d933ddd503a1ca0a7c562c51c503139c541e707e/lib/sqlalchemy/testing/assertions.py#L465
3) Load the actual tables as dataframes and compare them with expected 
dataframes using pandas assert_frame_equal
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.testing.assert_frame_equal.html

Any recommendations / thoughts would be much appreciated, both as to the 
approach and the implementation :-)

-- 
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/d786a59c-b995-4614-8023-bd27f20b2dee%40googlegroups.com.