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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:31:10+00:00 2026-05-23T03:31:10+00:00

Assume the following in MySQL: CREATE TABLE users ( id integer auto_increment primary key,

  • 0

Assume the following in MySQL:

CREATE TABLE users (
  id integer auto_increment primary key,
  username varchar(30),
  active enum('N','Y'),
  created_on int(11),
  updated_on int(11),
  points int(10),
  // other fields
);

CREATE TABLE comments (
  id integer auto_increment primary key,
  user_id integer,
  forum_id integer,
  favorited integer,
  // other fields
);

Note that no formal foreign key constraints are added to the tables. This is something I’ve inherited and cannot change on our current setup. (We’re overhauling the whole system, but in the meantime I have to work with what I’ve been given)

I’m having trouble wrapping my head around SQLalchemy’s joins when there’s no formal foreign key established between tables.

Effectively, I’d like to do something like:

SELECT 
  u.username,
  c.forum_id,
  count(c.id)
FROM 
  users u
  JOIN comments c ON u.id=c.user_id
WHERE
  u.id = 1234
GROUP BY
  u.username,
  c.forum_id;

Code I have includes things like the following:

mapper(Users, users, primary_key=[users.c.id],
    include_properties=['user_id', 'username', 'active', 'created_on',
        'updated_on', 'points'])
mapper(Comments, comments, primary_key=[comments.c.id],
    include_properties=['active', 'user_id', 'favorited', 'forum_id'])

j = join(users, comments)
mapper(UserComments, j, properties={'user_id': [users.c.id,
    comments.c.user_id]})

session = create_session()
query = session.query(UserComments).filter(users.cid == 1234)
rdata = run(query)
for row in rdata:
    print row

… which of course fails with:

sqlalchemy.exc.ArgumentError: Can't find any foreign key relationships
between 'users' and 'comments'.

I’m not sure how to work around this when we have no foreign keys. How else do I define the relationship? I thought it was part of the mapper() call:

mapper(UserComments, j, properties={'user_id': [users.c.id, 
    comments.c.user_id]})

… but apparently I’ve misread the documentation.

Thanks in advance for any help.

  • 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-23T03:31:10+00:00Added an answer on May 23, 2026 at 3:31 am

    You have two options. You can pass the join condition in join like so:

    j = join(users, comments, onclause=users.c.id == commends.c.user_id)
    

    If you’re defining this in terms of a orm.relationship property, the keyword parameter will be primaryjoin instead of onclause.

    However, the approach I Prefer is to just lie. Inform SQLAlchemy that there is a foreign key, even though there is not. 

    comments = Table('comments', metadata,
        Column('id', Integer, primary_key=True),
        Column('user_id', Integer, ForeignKey('users.id')),
        ...
    )
    

    SQLAlchemy will the proceed as if the foreign key were in fact present, even though the actual database doesn’t have that. Of course, you may run into trouble if the implied foriegn key constraint is violated (comments.user_id when there’s no corresponding users.id), but you’d probably be in trouble anyway.

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

Sidebar

Related Questions

Let's assume I have 3 tables in a MySQL table with the following fields.
Assume a table with the following columns: pri_id , item_id , comment , date
Let's assume we've got the following Java code: public class Maintainer { private Map<Enum,
I have a table that looks something like this: CREATE TABLE student_results(id integer, name
Suppose I have the following tables: CREATE TABLE Game ( GameID INT UNSIGNED NOT
Assume I have a table that has the following structure: ================= | Id |
I have a table with about 1.4 billion records, with the following format: mysql>
I have a question about MySQL InnoDB. For example: I have the following table
I want to create an application using php and mysql. Assume i am making
For the sake of simplicity, let's assume the following tables exist: Table 1 -

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.