[Sqlalchemy-tickets] Issue #4014: SQLA don't set FK in relationship if inheritance present (zzzeek/
Brought to you by:
zzzeek
From: Вася Г. <iss...@bi...> - 2017-06-21 09:29:45
|
New issue 4014: SQLA don't set FK in relationship if inheritance present https://bitbucket.org/zzzeek/sqlalchemy/issues/4014/sqla-dont-set-fk-in-relationship-if Вася Гайкин: I want build pseudo file system on DB tables and make several classes. But assign to **item.parent** attribute has no effect. It work if write **item.parent_id = parent.id**. *(in attanchment simple example project)* ``` #!python class Item( Base ): name = Column( String, nullable=False ) parent_id = Column( Integer, ForeignKey( 'Item.id', onupdate='cascade', ondelete='cascade', deferrable=True ), nullable=False ) parent = relationship( 'Folder', foreign_keys=parent_id, uselist=False, post_update=True ) _type = Column( String, nullable=False ) __mapper_args__ = { 'polymorphic_on': _type } class File( Item ): id = Column( Integer, ForeignKey( 'Item.id', onupdate='cascade', ondelete='cascade' ), primary_key=True ) data = Column( LargeBinary, nullable=False ) mime = Column( String, nullable=False ) preview_id = Column( Integer, ForeignKey( 'File.id', onupdate='cascade', ondelete='cascade' ) ) preview = relationship( 'File', foreign_keys=preview_id, uselist=False ) __mapper_args__ = { 'polymorphic_identity': 'file' } ``` **part of utils.py** ``` #!python def load_file( path, parent ): data = open( path, 'rb' ).read() name = os.path.basename( path ) file = File() file.name = name file.data = data file.mime = 'image/jpeg' file.parent_id = parent.id # <--- OK # file.parent = parent # <--- FAIL session.add( file ) session.commit() return file ``` **part of run.py** ``` #!python main_file = load_file( '/tmp/example_2.jpg', usr ) preview = load_file( '/tmp/example_3.jpg', bin_ ) main_file.preview = preview # <--- OK link = Link() link.target = root link.name = main_file.name # link.parent_id = root.id # <--- OK link.parent = root # <--- FAIL session.add( link ) session.commit() ``` |