Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5988859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:01:06+00:00 2026-05-22T23:01:06+00:00

I have some code here. I recently added this root_id parameter. The goal of

  • 0

I have some code here. I recently added this root_id parameter. The goal of that is to let me determine whether a File belongs to a particular Project without having to add a project_id FK into File (which would result in a model cycle.) Thus, I want to be able to compare Project.directory to File.root. If that is true, File belongs to Project.

However, the File.root attribute is not being autogenerated for File. My understanding is that defining a FK foo_id into table Foo implicit creates a foo attribute to which you can assign a Foo object. Then, upon session flush, foo_id is properly set to the id of the assigned object. In the snippet below that is clearly being done for Project.directory, but why not for File.root?

It definitely seems like it has to do with either 1) the fact that root_id is a self-referential FK or 2) the fact that there are several self-referential FKs in File and SQLAlchemy gets confused.

Things I’ve tried.

  • Trying to define a ‘root’ relationship() – I think this is wrong, this should not be represented by a join.
  • Trying to define a ‘root’ column_property() – allows read access to an already set root_id property, but when assigning to it, does not get reflected back to root_id

How can I do what I’m trying to do? Thanks!

from sqlalchemy import create_engine, Column, ForeignKey, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import backref, relationship, scoped_session, sessionmaker, column_property

Base = declarative_base()
engine = create_engine('sqlite:///:memory:', echo=True)
Session = scoped_session(sessionmaker(bind=engine))

class Project(Base):
   __tablename__ = 'projects'
   id = Column(Integer, primary_key=True)
   directory_id = Column(Integer, ForeignKey('files.id'))

class File(Base):
   __tablename__ = 'files'
   id = Column(Integer, primary_key=True)
   path = Column(String)
   parent_id = Column(Integer, ForeignKey('files.id'))
   root_id = Column(Integer, ForeignKey('files.id'))
   children = relationship('File', primaryjoin=id==parent_id, backref=backref('parent', remote_side=id), cascade='all')

Base.metadata.create_all(engine)

p = Project()
root = File()
root.path = ''

p.directory = root
f1 = File()
f1.path = 'test.txt'
f1.parent = root
f1.root = root

Session.add(f1)
Session.add(root)
Session.flush()
# do this otherwise f1 will be returned when calculating rf1
Session.expunge(f1)

rf1 = Session.query(File).filter(File.path == 'test.txt').one()
# this property does not exist
print rf1.root
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T23:01:08+00:00Added an answer on May 22, 2026 at 11:01 pm

    My understanding is that defining a FK foo_id into table Foo implicit creates a foo attribute to which you can assign a Foo object.

    No, it doesn’t. In the snippet, it just looks like it is being done for Project.directory, but if you look at the SQL statements being echo’ed, there is no INSERT at all for the projects table.

    So, for it to work, you need to add these two relationships:

    class Project(Base):
        ...
        directory = relationship('File', backref='projects')
    
    class File(Base):
        ...
        root = relationship('File', primaryjoin='File.id == File.root_id', remote_side=id)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have some old C code here that's built with nmake. Is there an
Here's my problem - I have some code like this: <mx:Canvas width=300 height=300> <mx:Button
Here's the situation: We have some generic graphics code that we use for one
I have a piece of code here that i really could use some help
I've recently added this little bit of code to my .htaccess file: RewriteCond %{REQUEST_FILENAME}
I have some code where I'm returning an array of objects. Here's a simplified
I have seen the following code: [DefaultValue(100)] [Description(Some descriptive field here)] public int MyProperty{...}
I have some code like this in a winforms app I was writing to
I have some code like this: If key.Equals(search, StringComparison.OrdinalIgnoreCase) Then DoSomething() End If I
I got this code on here recently <script type='text/javascript'> $('#discountselection').hide(); $('#No').click(function(){ $('#discountselection').hide(); }); $('#Yes').click(function(){

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.