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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:05:33+00:00 2026-05-20T05:05:33+00:00

I have a Django-created tables in PostgreSQL 8.4 database, where one table extends another.

  • 0

I have a Django-created tables in PostgreSQL 8.4 database, where one table “extends” another. One table (FooPayment) has primary key, which references another table (Payment). In SQL it looks like this:

CREATE TABLE foo.payments_payment
(
  id integer NOT NULL DEFAULT nextval('payments_payment_id_seq'::regclass),
  user_id integer NOT NULL,
  ...
  CONSTRAINT payments_payment_pkey PRIMARY KEY (id),
  CONSTRAINT payments_payment_user_id_fkey FOREIGN KEY (user_id)
      REFERENCES auth.auth_user (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION DEFERRABLE INITIALLY DEFERRED
)

CREATE TABLE foo.payments_foopayment
(
  payment_ptr_id integer NOT NULL,
  ...
  CONSTRAINT payments_foopayment_pkey PRIMARY KEY (payment_ptr_id),
  CONSTRAINT payments_foopayment_payment_ptr_id_fkey FOREIGN KEY (payment_ptr_id)
      REFERENCES foo.payments_payment (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION DEFERRABLE INITIALLY DEFERRED,
  ...
)

However, I’ve not to use Django ORM for various reasons, and I’m trying to access the tables from SQLAlchemy (I’m using version 0.6.6, as was installed with pip):

# Base = declarative_base()
...

class Payment(Base):
    __tablename__ = 'payments_payment'
    __table_args__ = {'schema': 'foo', 'autoload': True}
    user = relation(User, backref='payments')

class FooPayment(Payment):
    __tablename__ = 'payments_foopayment'
    __table_args__ = {'schema': 'foo', 'autoload': True}

When I’m doing this as superuser, everything works. When I’m connecting as low-privileged user I get an exception:

Traceback (most recent call last):
  File "./test.py", line 3, in <module>
    from foos import models
  File "./foos/models.py", line 127, in <module>
    class FooPayment(Payment):
  File "lib/python2.6/site-packages/sqlalchemy/ext/declarative.py", line 1167, in __init__
    _as_declarative(cls, classname, cls.__dict__)
  File "lib/python2.6/site-packages/sqlalchemy/ext/declarative.py", line 1099, in _as_declarative
    ignore_nonexistent_tables=True)
  File "lib/python2.6/site-packages/sqlalchemy/sql/util.py", line 260, in join_condition
    "between '%s' and '%s'.%s" % (a.description, b.description, hint))
sqlalchemy.exc.ArgumentError: Can't find any foreign key relationships between 'payments_payment' and 'payments_foopayment'.

When I’m connecting as this low-privileged user with PgAdmin3, I see the relationship in GUI. I can also see it with this statement, SQLAlchemy issues itself:

SELECT conname, pg_catalog.pg_get_constraintdef(oid, true) as condef
    FROM  pg_catalog.pg_constraint r
    WHERE r.conrelid = 16234 AND r.contype = 'f'
    ORDER BY 1

Which properly returns a row, containing

"payments_foopayment_payment_ptr_id_fkey"; "FOREIGN KEY (payment_ptr_id) REFERENCES payments_payment(id) DEFERRABLE INITIALLY DEFERRED"

As for database permissions, both payments_payment and payments_foopayment are GRANTed SELECT and UPDATE. I’ve tried temporarily granting all permissions on them, without any success. If this matters, a seqence payments_payment_id_seq is GRANTed for SELECT and USAGE. Obviously, schema foo is GRANTed for USAGE.

How should I either define the relationship manually in Python, or do something on DB side, so introspection would work for non-privileged user?

Hints on debugging the problem are also very much welcomed, as I’m completely lost in SA internals.

  • 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-20T05:05:34+00:00Added an answer on May 20, 2026 at 5:05 am

    You can log the queries of SQLAlchemy, and compare what happens with different users.

    import logging
    
    # Early in your main()
    logging.basicConfig()
    logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)
    logging.getLogger('sqlalchemy.orm').setLevel(logging.INFO)
    

    logging.DEBUG logs the response data as well.

    As far as I can tell, for reflection, SQLAlchemy uses table OIDs and queries pg_catalog; you gave an example. The code is in SQLAlchemy.dialects.postgresql.base.

    If the permissions for autoload give you grief, you can declare the relationship in code with something like this:

    class FooPayment(Payment):
        payment_ptr_id = Column(Integer, ForeignKey(Payment.id), primary_key=True)
        payment = relationship(
                      Payment, foreign_keys=[payment_ptr_id], backref='foo_payment')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Django model with a large number of fields and 20000+ table
I have done a little Django development, but it has all been in a
I have model Foo which has field bar. The bar field should be unique,
Our website uses a PHP front-end and a PostgreSQL database. We don't have a
I have the following multi-table inheritance situation: from django.db import Models class Partner(models.Model): #
I am having problem in django. I have created a form in my app
I'm working with a sqlite database, using python/django. I would need to have my
I have a script that appends some rows to a table. One of the
I am trying to install django on Webfaction, and i have one all the
I am trying to use django-tables to create a sortable table out of some

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.