you’re getting confused over attributes that refer to foreign key columns and attributes that refer to relationships.
if you have a class as:
class Site(Base):
# …
fk_school_id = Column(Integer, ForeignKey(‘school.id’))
school = relationship(School)
then say you have a Site:
my_site = Site()
you can:
A. assign scalar, typically integer values to the foreign key:
my_site.fk_school_id = 17
or
B. assign objects to the relationship attribute:
my_school = Session.query(School).get(17)
my_site.school = my_school
what you’re doing is this:
my_site.fk_school_id = my_school # <— wrong
On Feb 8, 2014, at 7:14 AM, Jude Lucien <[email protected]> wrote:
>
> This approach still doesn't seem to help me. I get the same error, just
> about type School:
>
> 2014-02-08 11:33:59,109 INFO sqlalchemy.engine.base.Engine ROLLBACK
> (ProgrammingError) can't adapt type 'School' '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' {'city': u'Carddif',
> 'fk_school_id': <warrior.models.location.School object at 0x9abcf8c>, 'name':
> u'Choo Site', 'address1': u'1 Choo Way', 'address2': u'', 'postcode':
> u'12345', 'fk_country_id': u'1'}
>
> How can I use relationships to do this? If I do:
>
> school = School()
> site = Site()
> site.fk_school_id = school
>
> I get the above error. If I change school to school.id I get the
> InstrumentedAttribute error again.
>
> How to do this without doing multiple commits?
>
> On 5 February 2014 22:34, Matthew Phipps <[email protected]> wrote:
>
> On Wednesday, February 5, 2014 10:44:39 AM UTC-5, Michael Bayer wrote:
>
> On Feb 5, 2014, at 9:43 AM, Jude Lucien <[email protected]> wrote:
>
>> 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?
>
> Base.metadata is where you’d call create_all(my engine) from.
>
>
> @Jude: db.create_all() is part of the Flask-SQLAlchemy API, not SQLAlchemy
> proper. AFAIK it's ok to mix the two together (the project I'm working on
> certainly does) but if you're not subclassing db.Model in your models
> Flask-SQLAlchemy may not be doing you much good. Which is fine, just be aware.
>
> -Matt
>
> --
> You received this message because you are subscribed to a topic in the Google
> Groups "sqlalchemy" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/sqlalchemy/yl_5BnwzfhA/unsubscribe.
> To unsubscribe from this group and all its topics, 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.
>
>
>
> --
> "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 [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.
signature.asc
Description: Message signed with OpenPGP using GPGMail
