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 6376991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:48:49+00:00 2026-05-25T01:48:49+00:00

Suppose I have a table nodes where I store a tree. Each node has

  • 0

Suppose I have a table “nodes” where I store a tree. Each node has a primary key id and a column parent_id.
Of course, I want to access a parent attribute of each node instance, that is, a relation.
One might try:

import sqlalchemy, sqlalchemy.orm, sqlalchemy.ext.declarative
engine = sqlalchemy.create_engine('sqlite:///PATHTOMYDATABASE', echo=True)
Base = sqlalchemy.ext.declarative.declarative_base()
class Node(Base):
    __tablename__ = "nodes"
    id = sqlalchemy.Column(sqlalchemy.Integer, primary_key=True) 
    parent_id = sqlalchemy.Column(sqlalchemy.Integer, sqlalchemy.ForeignKey("nodes.id"))
    parent = sqlalchemy.orm.relationship("Node", backref=sqlalchemy.orm.backref("childs"))
Base.metadata.create_all(engine)

But when I do that, I get an error:

sqlalchemy.exc.InvalidRequestError: Table 'nodes' is already defined for this MetaData instance. Specify 'useexisting=True' to redefine options and columns on an existing Table object.

I don’t understand at what point I could set that option 'useexisting=True'. Is this the right way?

EDIT: In fact, the error comes indirectly from another part of the original script. If one replaces the database path by the temporary database :memory:, it works without problems. Thanks to TokenMacGuy.

So the above example can be considered as a working example.

  • 1 1 Answer
  • 3 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-25T01:48:50+00:00Added an answer on May 25, 2026 at 1:48 am

    For some reason, you already have a class registered for that table, or you have defined the table (possibly indirectly) twice. This happens to me most frequently when I’m experimenting on the python command line; sqlalchemy remembers the table definitions and class mappings longer than is immediately obvious (from the lifetime of the actual MetaData instance). Make sure you are defining the table only once (exit out of the python interpreter and restart it, if you are in one). Other causes why they might be already present is from table reflection, or something has called reload() without really clearing out sys.modules first.

    If you are using table reflection, you pass the useexisting=True option to the declarative extension in the __table_args__ class variable:

    class Node(Base):
        __tablename__ = "nodes"
        __table_args__ = {"useexisting": True}
    

    But only do that if you are sure that you are defining the table intentionally before you define the python class.

    If none of these seem to fix the problem, please reduce your module to a minimal example that shows the problem and then post the whole thing in your answer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a table called Companies that has a DepartmentID column. There's also
Suppose I have table Person table Employee, which inherits Person. I want to get
Suppose I have a tags table with two columns: tagid and contentid . Each
Suppose I have table first_table which has an FK to table second_table table second_table
suppose I have this table var 10 3 4 60 70 and I want
Suppose you have a table widget class. Do you do table.row(i).column(0).setText(students[i].surname()) or table[0][i] =
Suppose I have table tags which has a field count that indicates how many
Suppose i have this table table (a,b,c,d). Datatypes are not important. I want to
Suppose I have a table a with one column b and three rows(1,2,3), I
Suppose I have table A with a field that can be either 1 or

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.