I'm sorry, but my conclusion about string mismatch was completely wrong. I had a bug somewhere else (what I could not see) and the only difference between objects I noticed was the type of strings.
So it works. Thank you. On Monday, February 24, 2014 2:34:42 PM UTC+1, Simon King wrote: > > Apart from putting unicode strings on your non-persistent object, you > mean? > > What version of Python are you using? Under Python 2, I would expect a > unicode string to compare equal to a non-unicode string as long as the > contents are equivalent. > > ie. > > >>> "abcdefg" == u"abcdefg" > True > > (On Python 3 it might be more complicated, but I don't use Python 3 so > I can't really help you.) > > If you are using Python 2 and the strings don't compare equal, perhaps > you have an incorrect encoding defined somewhere? > > Simon > > On Mon, Feb 24, 2014 at 12:59 PM, <[email protected] <javascript:>> > wrote: > > I've tried using it but I failed. I made my model classes inherit > > ComparableEntity. Unfortunately comparison of persistent object with the > > non-persistent fails, as the former has unicode string while the latter > does > > not. How can I fix it? > > > > Marcin > > > > On Monday, February 24, 2014 1:13:49 PM UTC+1, Simon King wrote: > >> > >> On Mon, Feb 24, 2014 at 11:59 AM, <[email protected]> wrote: > >> > Hi > >> > > >> > Is there a way to compare mapped objects by values and not by > identity? > >> > I > >> > want to create an instance of some mapped class, set its attributes > >> > (including relationship one to many) to some values and then ask > >> > SQLAlchemy > >> > to check if there is such object in database. > >> > > >> > Let me explain wider context of what I am trying to do. I am writing > >> > component test for my application. The test should create a set of > data, > >> > save the data in .csv file, make application read the file and then > >> > verify > >> > that proper records were inserted into database. So I was going to > use > >> > classes that are mapped to database as my model, create some objects, > >> > set > >> > their attributes, create .csv file based on these objects and then > use > >> > the > >> > objects to check if data was properly stored in database. > >> > > >> > A more precise example. Let's assume we have two classes: > >> > > >> > class User(Base): > >> > __tablename__ = 'users' > >> > > >> > id = Column(Integer, primary_key=True) > >> > name = Column(String) > >> > fullname = Column(String) > >> > password = Column(String) > >> > addresses = relationship("Address", order_by="Address.id", > >> > backref="user") > >> > > >> > class Address(Base): > >> > __tablename__ = 'addresses' > >> > > >> > id = Column(Integer, primary_key=True) > >> > email_address = Column(String, nullable=False) > >> > user_id = Column(Integer, ForeignKey('users.id')) > >> > > >> > I want to create instance of User, set its name, fullname, password > and > >> > add > >> > a few addresses. Now I want to query database for object with exactly > >> > the > >> > same properties including same addresses. How can I do it? Is it > >> > possible to > >> > avoid explicit comparisons of each field? > >> > > >> > Regards > >> > Marcin > >> > > >> > >> The SQLAlchemy unit tests use a mixin class called ComparableEntity > >> which would allow you to compare an object you've just constructed > >> with one you've loaded from the database: > >> > >> > >> > https://bitbucket.org/zzzeek/sqlalchemy/src/8b58c6adc2f000f6ce69e8107d10af3ee6e304ba/lib/sqlalchemy/testing/entities.py?at=master#cl-35 > > >> > >> However, it would require you to load the object that you want to > >> compare against from the database. This is more complicated given that > >> you want to compare related objects as well, but perhaps it would be > >> fine if you were to query the "primary" object (eg. User in your > >> example above) and then allow SA to load the associated Addresses > >> automatically. > >> > >> Hope that helps, > >> > >> Simon > > > > -- > > 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] <javascript:>. > > To post to this group, send email to > > [email protected]<javascript:>. > > > Visit this group at http://groups.google.com/group/sqlalchemy. > > For more options, visit https://groups.google.com/groups/opt_out. > -- 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 http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/groups/opt_out.
