[sqlalchemy] Cascade relationship from child to parent

2018-09-14 Thread Edans Sandes
Hello, 

I got stuck on a problem related to relatioships and cascades. In the docs 
, we have the 
following explanation:

Cascades:

 

*Mappers support the concept of configurable cascade behavior 
on relationship() 

 constructs. This 
refers to how operations performed on a “parent” object relative to a 
particular Session 

 should 
be propagated to items referred to by that relationship (e.g. “child” 
objects), and is affected by the relationship.cascade 

 option.*



It seems to me that the cascade attribute works only in the parent->child 
direction. Right now, I'm trying to figure out how to create one child with 
its parent_id, followed by an automatic refresh of its parent children list 
(i.e. without appending the child to the parent children list). Maybe it is 
not a good practice to use parent ids in ORM, but I'm wondering if it is 
possible to do it so. Sometimes we only want to create a child to a 
parent_id, without loading the parent object.


Let me show the source code that reproduce my problem:

from sqlalchemy import Table, Column, Integer, String, ForeignKey
from sqlalchemy.orm import relationship, backref, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine

Base = declarative_base()

Base = declarative_base()

class Parent(Base):
__tablename__ = 'parent'
parent_id = Column(Integer, primary_key=True)
children = relationship("Child", back_populates="parent", cascade="all")

class Child(Base):
__tablename__ = 'child'
child_id = Column(Integer, primary_key=True)
name = Column(String)
parent_id = Column(Integer, ForeignKey('parent.parent_id'))
parent = relationship("Parent", back_populates="children", 
cascade="all")

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

s = Session()

p = Parent()
c1 = Child(name="C1")
p.children.append(c1)

s.add(p)
s.flush()

print([x.name for x in p.children])  # ['C1']

c2 = Child(parent_id=1, name="C2")
s.add(c2)
s.flush()

print([x.name for x in p.children])  # ['C1']

s.refresh(c2.parent) # This solves my problem, but I would like to know how 
to do it automatically without an explicit refresh

print([x.name for x in p.children])  # ['C1','C2']

s.close()



In this code, when I create child "C2" pointing it to parent_id=1, the 
parent object (children relationship) is not refreshed automatically. So, 
it only prints the first child: C1. After the refresh, I see both children 
C1 and C2.
Is there any option that refresh the parent authomatically? Is there 
anything that I'm missing?


Thanks!
Edans Sandes

-- 
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 post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] Controlling table dependency for flushing

2018-09-14 Thread Simon King
In that case can you show us the code that is causing the problem?
On Fri, Sep 14, 2018 at 2:55 PM Alex Rothberg  wrote:
>
> I am not generating any IDs myself and I already have relationships between 
> the models.
>
> On Friday, September 14, 2018 at 4:33:08 AM UTC-4, Simon King wrote:
>>
>> On Thu, Sep 13, 2018 at 10:50 PM Alex Rothberg  wrote:
>> >
>> > Is it possible to hint at sqla the order in which it should write out 
>> > changes to the DB?
>> >
>> > I am having issues in which I add two new objects to a session, a and b 
>> > where a depends on b, but sqla is flushing a before b leading to an fk 
>> > issue. I can solve this a few ways: explicitly calling flush after adding 
>> > b, or changing the fk constraint to be initially deferred. Ideally I would 
>> > not have to do either of these.
>> >
>>
>> If you have configured a relationship between the two classes
>> (http://docs.sqlalchemy.org/en/latest/orm/tutorial.html#building-a-relationship),
>> and you've linked the objects together using that relationship (a.b =
>> b), then SQLAlchemy will flush them in the correct order. If you are
>> generating your IDs in Python and assigning them to the primary and
>> foreign key columns directly, SQLAlchemy probably won't understand the
>> dependency.
>>
>> Does using a relationship fix your problem?
>>
>> Simon
>
> --
> 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 post to this group, send email to sqlalchemy@googlegroups.com.
> 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 sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


[sqlalchemy] Sqlachemy util drop_database Programming error

2018-09-14 Thread Madhav Dube
Running sqlalchemy in test environment keep on giving the weird error:

The code in teardown method:

from sqlalchemy_utils import drop_database
drop_database(TEST_URI)


(psycopg2.ProgrammingError) must be a member of the role whose process is 
being terminated

Wondering if there is a reason for this error or it is just a hiccup?

My SO question:
intermittent-sqlalchemy-drop-database-error-psycopg2-programmingerror-must-be 


Might be related to open issue: 
https://github.com/Overseas-Student-Living/sqlalchemy-diff/issues/10

Thanks

-- 
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 post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] Controlling table dependency for flushing

2018-09-14 Thread Alex Rothberg
I am not generating any IDs myself and I already have relationships between 
the models.

On Friday, September 14, 2018 at 4:33:08 AM UTC-4, Simon King wrote:
>
> On Thu, Sep 13, 2018 at 10:50 PM Alex Rothberg  > wrote: 
> > 
> > Is it possible to hint at sqla the order in which it should write out 
> changes to the DB? 
> > 
> > I am having issues in which I add two new objects to a session, a and b 
> where a depends on b, but sqla is flushing a before b leading to an fk 
> issue. I can solve this a few ways: explicitly calling flush after adding 
> b, or changing the fk constraint to be initially deferred. Ideally I would 
> not have to do either of these. 
> > 
>
> If you have configured a relationship between the two classes 
> (
> http://docs.sqlalchemy.org/en/latest/orm/tutorial.html#building-a-relationship),
>  
>
> and you've linked the objects together using that relationship (a.b = 
> b), then SQLAlchemy will flush them in the correct order. If you are 
> generating your IDs in Python and assigning them to the primary and 
> foreign key columns directly, SQLAlchemy probably won't understand the 
> dependency. 
>
> Does using a relationship fix your problem? 
>
> Simon 
>

-- 
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 post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] Controlling table dependency for flushing

2018-09-14 Thread Simon King
On Thu, Sep 13, 2018 at 10:50 PM Alex Rothberg  wrote:
>
> Is it possible to hint at sqla the order in which it should write out changes 
> to the DB?
>
> I am having issues in which I add two new objects to a session, a and b where 
> a depends on b, but sqla is flushing a before b leading to an fk issue. I can 
> solve this a few ways: explicitly calling flush after adding b, or changing 
> the fk constraint to be initially deferred. Ideally I would not have to do 
> either of these.
>

If you have configured a relationship between the two classes
(http://docs.sqlalchemy.org/en/latest/orm/tutorial.html#building-a-relationship),
and you've linked the objects together using that relationship (a.b =
b), then SQLAlchemy will flush them in the correct order. If you are
generating your IDs in Python and assigning them to the primary and
foreign key columns directly, SQLAlchemy probably won't understand the
dependency.

Does using a relationship fix your problem?

Simon

-- 
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 post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] Re: Hybrid Property vs Hybrid Method Expression Names

2018-09-14 Thread james
Thanks for the help - I had missed the "copy vs modifying in place" 
difference between hybrid_method and hybrid_property.

I think adding another dragon would be helpful here, probably located in 
http://docs.sqlalchemy.org/en/latest/orm/extensions/hybrid.html#defining-expression-behavior-distinct-from-attribute-behavior.
 
I'm happy to move this thread into a docs issue if that would be helpful.

On Thursday, September 13, 2018 at 5:45:05 PM UTC+2, Mike Bayer wrote:
>
> On Thu, Sep 13, 2018 at 7:55 AM,  > 
> wrote: 
> > Update: I have just found 
> > 
> http://docs.sqlalchemy.org/en/latest/changelog/migration_12.html#hybrid-attributes-support-reuse-among-subclasses-redefinition-of-getter
>  
> > which documents that getters and setters must have the same name as the 
> > original expression. 
> > 
> > Can I just check that it is expected for expressions to have this 
> > requirement? If so, is it worth opening a docs issue to add this to the 
> main 
> > hybrid property docs? 
>
> this is the mechanics of Python, when you say: 
>
> @mything.foobar 
> def _myotherthing(...) 
>
>
> you are assigning to the name "_myotherthing".   Since 
> @hybrid_property now creates a copy when any modifier is called, the 
> original hybrid you have at "mything" was not changed. 
>
> All the documentation examples at 
> http://docs.sqlalchemy.org/en/latest/orm/extensions/hybrid.html 
> indicate using the same method name for each modification.I can 
> add more dragons to the hybrid docs as well clarifying that this 
> naming scheme is intentional and required, if that helps. 
>
>
>
> > 
> > -- 
> > 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+...@googlegroups.com . 
> > To post to this group, send email to sqlal...@googlegroups.com 
> . 
> > 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 sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.