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

  • SEARCH
  • Home
  • 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 6824007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:51:04+00:00 2026-05-26T21:51:04+00:00

I’m having a great time learning Python, but I’ve just gotten a bit stuck

  • 0

I’m having a great time learning Python, but I’ve just gotten a bit stuck on trying to incorporate a recursive function into SQLAlchemy.

Essentially, have a function which creates a instance of a class to be put in the database. Inside this function, I get user input on whether the instance has a parent class (defined using a self-referential adjacency table). If it does, the function is then called again, recursively. This function seems to work if no parent class is needed, but whenever the recursive element is activated it crashes.

My code is like so:

engine = create_engine('sqlite:///recDB.db')
Session = sessionmaker(bind=engine)
session = Session()
Base = declarative_base()

class IngList(Base):
    __tablename__ = "ingList"

    id = Column(Integer, primary_key = True)
    ingredient = Column(String, nullable=False)
    parentIng = Column(Integer, ForeignKey('ingList.id'))
    children = relationship("IngList",backref=backref('parent', remote_side=[id]))

    def __init__(self, ingredient):
        self.ingredient = ingredient

def addIngredient(ingredient):
    global newIngList
    newIng = IngList(ingName) #create new class instance
    parentIng = raw_input("To add parent ingredient, type it.  Otherwise press enter")
    if parentIng != '':
        parentIngObj = addIngredient(parentIng) # Recursion!
        newIng.parentIng = parentIngObj
    newIngList.append(newIng)

if __name__ == '__main__':
    newIngList = []
    ingredient = raw_input("Enter new ingredient")
    addIngredient(ingredient)
    for ing in newIngList
        session.add(ing)
    session.commit()

I’ve kept this sample simple to keep it readable, but if I’m missing some important information, please let me know.

I assumed that my problem was that the class instances were losing scope as I recursed, but adding the global to the list variable did not seem to fix that. I also thought that the fact that a session acts as sort of a buffer would handle any scope problems.

Does this have something to do with eager loading? I saw that in the documentation, but don’t really understand it.

The error I’m getting is:

Traceback (most recent call last):
  File "C:\workspace\recipes\langProc.py", line 102, in <module>
    session.commit()
  File "c:\Python27\lib\site-packages\sqlalchemy\orm\session.py", line 645, in commit
    self.transaction.commit()
  File "c:\Python27\lib\site-packages\sqlalchemy\orm\session.py", line 313, in commit
    self._prepare_impl()
  File "c:\Python27\lib\site-packages\sqlalchemy\orm\session.py", line 297, in _prepare_impl
    self.session.flush()
  File "c:\Python27\lib\site-packages\sqlalchemy\orm\session.py", line 1547, in flush
    self._flush(objects)
  File "c:\Python27\lib\site-packages\sqlalchemy\orm\session.py", line 1616, in _flush
    flush_context.execute()
  File "c:\Python27\lib\site-packages\sqlalchemy\orm\unitofwork.py", line 328, in execute
    rec.execute(self)
  File "c:\Python27\lib\site-packages\sqlalchemy\orm\unitofwork.py", line 472, in execute
    uow
  File "c:\Python27\lib\site-packages\sqlalchemy\orm\mapper.py", line 2153, in _save_obj
    execute(statement, params)
  File "c:\Python27\lib\site-packages\sqlalchemy\engine\base.py", line 1399, in execute
    params)
  File "c:\Python27\lib\site-packages\sqlalchemy\engine\base.py", line 1532, in _execute_clauseelement
    compiled_sql, distilled_params
  File "c:\Python27\lib\site-packages\sqlalchemy\engine\base.py", line 1640, in _execute_context
    context)
  File "c:\Python27\lib\site-packages\sqlalchemy\engine\base.py", line 1633, in _execute_context
    context)
  File "c:\Python27\lib\site-packages\sqlalchemy\engine\default.py", line 330, in do_execute
    cursor.execute(statement, parameters)
sqlalchemy.exc.InterfaceError: (InterfaceError) Error binding parameter 0 
- probably unsupported type.
u'UPDATE "ingList" SET "parentIng"=? WHERE "ingList".id = ?' 
(<assignDB.IngList object at 0x00000000096969E8>, 4)
  • 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-26T21:51:04+00:00Added an answer on May 26, 2026 at 9:51 pm

    I think you have couple of bugs/typos which make your code not work. I assume that you created a small code sample to show the issue, and I hope that once you fix those in the original code, you problem will be solved as well:

    • instead of newIng.parentIng = parentIngObj you should have newIng.parent = parentIngObj. I believe this should solve the issue.
      So you have to assign the parent instance to the relationship object, and not to its key. Using J.F. Sebastian‘s suggestions could work too if the objects were already stored in the database, but new instances do not have the id assigned yet
    • addIngredient(...) has two issues:
      • typo: the parameter ingredient should be renamed to ingName or vice-versa
      • bigger issue: the addIngredient(...) does not return any value, so in fact you assign None to the parent side of the relationship.
        Again, given this is just a sample code, you might not have these issues in your real code.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want to construct a data frame in an Rcpp function, but when I
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.