yep... here's the error:
sqlalchemy.orm.exc.FlushError: Attempting to flush an item of type <class
'__main__.Thinker'> as a member of collection "Address.user". Expected an
object of type <class '__main__.User'> or a polymorphic subclass of this type.
If <class '__main__.Thinker'> is a subclass of <class '__main__.User'>,
configure mapper "Mapper|User|users" to load this subtype polymorphically, or
set enable_typechecks=False to allow any subtype to be accepted for flush.
enable_typechecks=False disables this check:
user = relationship("User", enable_typechecks=False,
backref=backref('addresses', order_by=id))
it just means that later on, when you hit some_address.user, you may get a User
back, not a Thinker (or you will, if it hasn't been expired. you can't rely
on it being consistent). If that's OK, then set the flag - it just wants to
check that this is what you intend.
On May 30, 2014, at 1:51 PM, Victor Olex <[email protected]> wrote:
> Hello all, long time no see...
>
> Is it OK to create classes, which inherit from mapped classes, but are not
> meant to be persited and how to do it as to avoid FlushError on related
> classes?
>
> from sqlalchemy import *
> from sqlalchemy.ext.declarative import declarative_base
> from sqlalchemy.orm import Session, relationship, backref
>
> Base = declarative_base()
>
> class User(Base):
> __tablename__ = 'users'
> id = Column(Integer, Sequence('user_id_seq'), primary_key=True)
> name = Column(String(50))
> fullname = Column(String(50))
> password = Column(String(12))
>
> class Address(Base):
> __tablename__ = 'addresses'
> id = Column(Integer, primary_key=True)
> email_address = Column(String, nullable=False)
> user_id = Column(Integer, ForeignKey('users.id'))
> user = relationship("User", backref=backref('addresses', order_by=id))
>
> class Thinker(User):
> thought = 'Thoughts are not to be persited'
>
>
> e = create_engine('sqlite:///', echo=True)
> Base.metadata.bind = e
> Base.metadata.create_all()
>
> t = Thinker(name='Descartes')
> s = Session(bind=e)
> s.add(t)
> s.commit() # no problem
> a = Address(user=t, email='[email protected]')
> a = Address(user=t, email_address='[email protected]')
> s.commit() # FlushError
>
> Thanks,
>
> V.
>
>
> --
> 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/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 [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/d/optout.