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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:37:05+00:00 2026-05-27T05:37:05+00:00

In user_models.py , I have this: class Users(Base): __tablename__ = ‘account_users’ id = Column(Integer,

  • 0

In user_models.py, I have this:

class Users(Base):
    __tablename__ = 'account_users'
    id = Column(Integer, primary_key = True)
    username = Column(String(255), nullable=False)    
Base.metadata.create_all(engine)

When I run this, I create a user table.

On my other file, groups_models.py, I have this:

class Groups(Base):
    __tablename__ = 'personas_groups'
    id = Column(Integer, primary_key = True)
    user_id = Column(Integer, ForeignKey('account_users.id')) #This creates an error!!!
    user = relationship('Users') #this probably won't work. But haven't hit this line yet.

Base.metadata.create_all(engine)

So, as you can see, I want to put a many-to-one relationship from groups -> users.

But when I run groups_models.py…I get this error:

sqlalchemy.exc.NoReferencedTableError: Foreign key associated with column 'personas_groups.user_id' could not find table 'account_users' with which to generate a foreign key to target column 'id'

If I put the two tables together in one file, I’m sure it could work…but because I split it into 2 files (which I absolutely have to)…I don’t know how to make ForeignKey relationships work anymore?

  • 1 1 Answer
  • 1 View
  • 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-27T05:37:05+00:00Added an answer on May 27, 2026 at 5:37 am

    The key is to use the same Base for both foreign keys, instead of creating a new one for each table.

    basetest.py

    from sqlalchemy import create_engine, ForeignKey
    from sqlalchemy.ext.declarative import declarative_base
    from sqlalchemy.orm import relationship, backref
    from sqlalchemy import Column, Integer, String
    from sqlalchemy import Table, Text
    
    engine = create_engine('mysql://test:test@localhost/test1',
                        echo=False)
    
    Base = declarative_base()
    

    user_models.py

    from sqlalchemy import Column, Integer, String
    from sqlalchemy import Table, Text
    
    
    #Base = declarative_base()
    from basetest import Base
    
    class Users(Base):
        __tablename__ = 'account_users'
        __table_args__ = {'extend_existing':True}
        id = Column(Integer, primary_key = True)
        username = Column(String(255), nullable=False)    
    
    Base.metadata.create_all(engine)
    

    groups_models.py

    from sqlalchemy import create_engine, ForeignKey
    from sqlalchemy.ext.declarative import declarative_base
    from sqlalchemy.orm import relationship, backref
    from sqlalchemy import Column, Integer, String
    from sqlalchemy import Table, Text
    
    from basetest import Base
    #Base = declarative_base()
    from test1 import Users
    
    class Groups(Base):
        __tablename__ = 'personas_groups'
        __table_args__ = {'extend_existing':True}
        id = Column(Integer, primary_key = True )
        user_id = Column(Integer, ForeignKey('account_users2.id')) #This creates an error!!!
        user = relationship(Users) #this probably won't work. But haven't hit this line yet.
    
    Base.metadata.create_all(engine)
    

    Make sure that you have same Base to create all related tables.

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

Sidebar

Related Questions

I have the following model classes declared by SQLAlchemy: class User(Base): id = Column(Integer,
I have a two models set up like this: class User < ActiveRecord::Base #
I've got a sqlalchemy model that is set up like this: class Entry(Base): __tablename__
I have a user model: class User < ActiveRecord::Base end This model is used
I have a model class like this: class Note(models.Model): author = models.ForeignKey(User, related_name='notes') content
I have a model, smth like this: class Action(models.Model): def can_be_applied(self, user): #whatever return
I have a model defined as below: class Example(models.Model): user = models.ForeignKey(User, null=True) other
I have the following model set up: class UserProfile(models.Model): Additional attributes for users. url
I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent =>
I have two dependent models with one to many association class Post < ActiveRecord::Base

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.