Thanks for your informative answer! I have a secondary problem now having changed my model to use declarative base - as in db.create_all() does not create my tables in the database.
How can I do this using the declarative base method? On 5 February 2014 04:36, Michael Bayer <mike...@zzzcomputing.com> wrote: > > On Feb 4, 2014, at 8:20 PM, Jude Lucien <jluc...@gmail.com> wrote: > > > I am using SQLAlchemy 0.9 with Python 2.7.6 and Flask. > > I receive form data, place it into object and add the objects to the > session. When I try to do a db.commit() I get a rollback which seems to due > to the relationship object in the INSERT statement. The SQL error is as > follows: > > INFO sqlalchemy.engine.base.Engine INSERT INTO site (name, address1, > address2, postcode, city, fk_country_id, fk_school_id) VALUES (%(name)s, > %(address1)s, %(address2)s, %(postcode)s, %(city)s, %(fk_country_id)s, > %(fk_school_id)s) RETURNING site.id > > INFO sqlalchemy.engine.base.Engine {'city': u'New York', 'fk_school_id': > <sqlalchemy.orm.attributes.InstrumentedAttribute object at 0x8c274ac>, > 'name': u'Site1', 'address1': u'2 York Way', 'address2': u'', 'postcode': > u'12345', 'fk_country_id': u'2'} > > INFO sqlalchemy.engine.base.Engine ROLLBACK (ProgrammingError) can't adapt > type 'InstrumentedAttribute' > > The relationship code in the model is as follows: > > class Site(db.Model): > id = db.Column(db.Integer, primary_key = True) > school = db.relationship('School', backref = 'site', uselist = False) > > What is it about the relationship object that causes the rollback? I am > new to SQLAlchemy and so have followed what it says in the documentation > regarding relationships. > > Both of the data types in the models (Site, School) are ints. > > > > there's nothing wrong with the configuration there, the error has to do > with the wrong kind of object being assigned to a database row, in this > case the class-bound attribute. > > So basically this: > > class Site(Base): > # ... > fk_school_id = Column(Integer, ForeignKey(...)) > > class School(Base): > id = Column(Integer, primary_key=True) > > site = Site() > site.fk_school_id = School.id # <-- incorrect, School is a class, not an > instance of School > > session.add(site) > session.commit() > > > a class-bound attribute like "School.id" is an InstrumentedAttribute. > your intent here is to assign an integer value to fk_school_id. > > But also, when using relationship() you will have an easier time if you > work with objects rather than foreign key attributes: > > some_school = School() > site.school = some_school > > > > > > > > > > -- > 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/groups/opt_out. > > > -- "None are more hopelessly enslaved than those who falsely believe they are free" -- Johann Wolfgang von Goethe -- 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/groups/opt_out.