[Flask] Establishing foreign key back to parent class

Tim Johnson tim at akwebsoft.com
Sun Feb 14 18:24:19 EST 2016


* Tim Johnson <tim at akwebsoft.com> [160213 13:48]:
<...> 
> # My working code is as follows:
> 
> # class Base(db.Model):
> 
> #     __abstract_ = True
> #     id = db.Column(db.Integer, primary_key=True)
> #     date_created = db.Column(db.DateTime, default=db.func.current_timestamp())
> #     date_modified = db.Column(db.DateTime, default=db.func.current_timestamp(),
> #                               onupdate=db.func.current_timestamp())

  Turns out that I was bitten by a self-inflicted ORM bug... in my
  original code as taken from the tutorial, I had truncated
  __abstract__ to __abstract_  ## one trailing underscore!
  
  Because __abstract__ was not defined as True sqlalchemy attempted
  to map out a "base" table and subsequently create a relationship -
  which failed and generated the exception.

  The use of "special class directives" in sqlalchemy is discussed
  here :
  http://docs.sqlalchemy.org/en/rel_0_8/orm/extensions/declarative.html

  live and learn ...
-- 
Tim 
http://www.akwebsoft.com, http://www.tj49.com


More information about the Flask mailing list