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 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
  • 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-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 we have a table Users that has only one column UserId which is
Suppose I have a table that has three integer fields and one string field
Suppose I have a table with some natural grouping and ordering, for example records
Suppose I have following table in my database: Tbl_Persons: Id Country Name 1 Australia
I have a method that is suppose to edit a xml file: public void
When you join tables which are distributed on the same key and used these
I'm seeing some strange performance issues on SQL Server 2008 with a nullable geography
I'm learning PHP and Zend Framework. The following PHP function is supposed to fill
I'm working on asp.net website. I'm not able to count the only null values

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.