How to verify an object is in database? How to compare objects by values?

1,526 views
Skip to first unread message

marcin.raf...@gmail.com

unread,
Feb 24, 2014, 6:59:35 AM2/24/14
to sqlal...@googlegroups.com
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

Simon King

unread,
Feb 24, 2014, 7:13:49 AM2/24/14
to sqlal...@googlegroups.com
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

marcin.raf...@gmail.com

unread,
Feb 24, 2014, 7:59:47 AM2/24/14
to sqlal...@googlegroups.com
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

Simon King

unread,
Feb 24, 2014, 8:34:42 AM2/24/14
to sqlal...@googlegroups.com
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
> --
> 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 http://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/groups/opt_out.

marcin.raf...@gmail.com

unread,
Feb 24, 2014, 8:45:11 AM2/24/14
to sqlal...@googlegroups.com
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.
Reply all
Reply to author
Forward
0 new messages