Re: [sqlalchemy] Unexpected is_modified result after referencing a relationship

2015-02-27 Thread Michael Bayer



 On Feb 27, 2015, at 4:48 PM, Eric Smith e...@esmithy.net wrote:
 
 To those more experienced than me, does this behavior make sense? If so, 
 could you further my education with an explanation?
 
 If I change an attribute, is_modified returns True (as expected).
 If I then reference a relationship, is_modified returns False (unexpected).
 
 My output for the following program (using SQLAlchemy 0.9.7) is:
 
 False
 True
 False
 
 Thanks,
 Eric
 
 
 from sqlalchemy import Column, Integer, String, create_engine, ForeignKey
 from sqlalchemy.ext.declarative import declarative_base
 from sqlalchemy.orm import sessionmaker, relationship
 
 
 Base = declarative_base()
 
 
 class Parent(Base):
 __tablename__ = 'parent'
 id = Column(Integer, primary_key=True, nullable=False)
 message = Column(String)
 children = relationship(Child, backref='parent')
 
 
 class Child(Base):
 __tablename__ = 'child'
 id = Column(Integer, primary_key=True, nullable=False)
 parent_id = Column(Integer, ForeignKey('parent.id'))
 
 
 def main():
 engine = create_engine('sqlite:///:memory:')
 Session = sessionmaker(bind=engine)
 Base.metadata.create_all(engine)
 
 db = Session()
 parent = Parent()
 db.add(parent)
 db.commit()
 
 print db.is_modified(parent)
 parent.message = hi
 print db.is_modified(parent)
 len(parent.children)  # Should this change is_modified?
 print db.is_modified(parent)

Turn on sql echoing, which is always the first step towards understanding ORM 
behavior, and you'll see that the call to parent.children invokes a lazy load 
operation, which is a Query hence it autoflushes first, thereby synchronizing 
the state of message and resetting the modified flag. 



 
 if __name__ == '__main__':
 main()
 -- 
 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 http://groups.google.com/group/sqlalchemy.
 For more options, visit https://groups.google.com/d/optout.

-- 
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 http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] relationship problem

2015-02-27 Thread Michael Bayer



 On Feb 26, 2015, at 5:56 AM, Julien Cigar jci...@ulb.ac.be wrote:
 
 On Wed, Feb 25, 2015 at 06:10:55PM -0500, Michael Bayer wrote:
 
 
 Julien Cigar jci...@ulb.ac.be wrote:
 
 On Thu, Feb 19, 2015 at 11:31:10AM -0500, Michael Bayer wrote:
 Julien Cigar jci...@ulb.ac.be wrote:
 
 On Thu, Feb 19, 2015 at 02:45:43PM +0100, Julien Cigar wrote:
 Hello,
 
 I'm using SQLAlchemy 0.9.8 with PostgreSQL and the reflection feature of
 SQLAlchemy.
 
 I have the following tables (only relevant parts are show):
 https://gist.github.com/silenius/390bb9937490730741f2
 
 and the problematic mapper is the one of my association object:
 https://gist.github.com/silenius/1559a7db65ed30a1b079
 
 SQLAlchemy complains with the following error:
 sqlalchemy.exc.InvalidRequestError: One or more mappers failed to
 initialize - can't proceed with initialization of other mappers.
 Original exception was: Could not locate any simple equality expressions
 involving locally mapped foreign key columns for primary join condition
 'pool_invite_result.pool_invite_pool_id = pool_invite.pool_id AND
 pool_invite.pool_id = pool.id' on relationship PoolAccountResult.pool.
 Ensure that referencing columns are associated with a ForeignKey or
 ForeignKeyConstraint, or are annotated in the join condition with the
 foreign() annotation. To allow comparison operators other than '==', the
 relationship can be marked as viewonly=True.
 
 The problem is that in the PoolAccountResult mapper I want a
 relationship to the Pool but the link is made through an intermediate
 table (pool_invite) ..
 
 Any idea how to handle this with SQLAlchemy ?
 
 Thanks :)
 
 Julien
 
 ... and I'm answering to myself: it seems to work with
 https://gist.github.com/silenius/e7e59c96a7277fb5879f 
 
 does it sound right ?
 
 Sure.  Also, you could use automap which does figure these out in simple 
 cases: http://docs.sqlalchemy.org/en/rel_0_9/orm/extensions/automap.html
 
 always with this, any idea why SQLAlchemy inserts NULL and
 NULL for my two relationship (line 51-79) instead of the pool_obj and
 dup.owner ids (line 89-90), https://dpaste.de/1Trz ..?
 
 getting a 404 on that link.
 
 Hi Mike,
 
 Thanks for your help!
 
 I took the time to make a complete test case, available from here
 https://gist.github.com/silenius/96d6ed2544d14753853f

That's a very strange use of secondary, and I'm not sure I understand the 
rationale for an odd schema like this.   It should be pretty clear that when a 
table is set up as secondary, it is only used as a linkage between those 
immediate classes and there are no features to track the state of this table 
otherwise as though it were a mapped class.   Using the same secondary table in 
two unrelated relationships is not the intended use.  

Nevertheless, if you manipulate between Pool and User, that has no impact 
whatsoever on PoolInviteResult... Especially since we're dealing with a 
secondary table and not a first class mapped entity.You should add event 
listeners as needed on attributes such that when an in-Python change occurs 
between Pool and User, the desired change occurs for PoolInviteResult as well.

 
 I'm using PostgreSQL, and I checked that all constraints are properly
 created on server-side but I haven't checked with sqllite:// 
 
 
 
 
 
 
 
 Thanks,
 Julien
 
 
 -- 
 Julien Cigar
 Belgian Biodiversity Platform (http://www.biodiversity.be)
 PGP fingerprint: EEF9 F697 4B68 D275 7B11  6A25 B2BB 3710 A204 23C0
 No trees were killed in the creation of this message.
 However, many electrons were terribly inconvenienced.
 
 -- 
 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 http://groups.google.com/group/sqlalchemy.
 For more options, visit https://groups.google.com/d/optout.
 
 
 
 -- 
 Julien Cigar
 Belgian Biodiversity Platform (http://www.biodiversity.be)
 PGP fingerprint: EEF9 F697 4B68 D275 7B11  6A25 B2BB 3710 A204 23C0
 No trees were killed in the creation of this message.
 However, many electrons were terribly inconvenienced.
 
 -- 
 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 http://groups.google.com/group/sqlalchemy.
 For more options, visit https://groups.google.com/d/optout.
 
 -- 
 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 

Re: [sqlalchemy] CheckConstraint in sqlalchemy-1.0.0dev

2015-02-27 Thread Christopher Singley


On Friday, 27 February 2015 09:52:36 UTC-6, Simon King wrote:

 On Fri, Feb 27, 2015 at 3:34 PM, Christopher Singley ch...@singleys.com 
 javascript: wrote: 
  I'm using the latest dev version of sqlalchemy against postgresql-9.4.1 
  
  I can't get SA to apply table-level CHECK constraints defined via 
  declarative, although I can manually add them via psql to tables created 
 by 
  SA Metadata.create_all() 
  
  Here's the code I'm testing with: 
  
  https://gist.github.com/csingley/7783779320359dd5f74c 
  
  Am I doing something wrong? 
  

 I think you need to put your constraint into the __table_args__ attribute: 

   
 http://docs.sqlalchemy.org/en/rel_0_9/orm/extensions/declarative/table_config.html
  

 Hope that helps, 

 Simon 


Yep, that's what I was missing!  Makes sense syntactically.  Thank you sir. 
 

-- 
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 http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


[sqlalchemy] Unexpected is_modified result after referencing a relationship

2015-02-27 Thread Eric Smith
To those more experienced than me, does this behavior make sense? If so, 
could you further my education with an explanation?

If I change an attribute, is_modified returns True (as expected).
If I then reference a relationship, is_modified returns False (unexpected).

My output for the following program (using SQLAlchemy 0.9.7) is:

False
True
False

Thanks,
Eric


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


Base = declarative_base()


class Parent(Base):
__tablename__ = 'parent'
id = Column(Integer, primary_key=True, nullable=False)
message = Column(String)
children = relationship(Child, backref='parent')


class Child(Base):
__tablename__ = 'child'
id = Column(Integer, primary_key=True, nullable=False)
parent_id = Column(Integer, ForeignKey('parent.id'))


def main():
engine = create_engine('sqlite:///:memory:')
Session = sessionmaker(bind=engine)
Base.metadata.create_all(engine)

db = Session()
parent = Parent()
db.add(parent)
db.commit()

print db.is_modified(parent)
parent.message = hi
print db.is_modified(parent)
len(parent.children)  # Should this change is_modified?
print db.is_modified(parent)

if __name__ == '__main__':
main()

-- 
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 http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.