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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:02:48+00:00 2026-05-31T10:02:48+00:00

Suppose we have a PostgreSQL database with two tables A, B. table A columns:

  • 0

Suppose we have a PostgreSQL database with two tables A, B.

table A columns: id, name
table B columns: id, name, array_a

The column array_a in table B contains a variable length array of ids from table A. In SQLAlchemy we have two classes that model those tables, say class A and B.

The following works fine to get all the objects A that are referenced in an object B:

session.query(A).join(B, A.id == func.any(B.array_a)).filter(B.id == <id>).all()

How can we create a relationship in B referencing the objects A corresponding to the array? Tried column comparators using the func.any above but it complains that ANY(array_a) is not a column in the model. Specifying the primaryjoin conditions as above doesn’t seem to cut it either.

  • 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-31T10:02:50+00:00Added an answer on May 31, 2026 at 10:02 am

    This anti-pattern is called “Jaywalking”; and PostgreSQL’s powerful type system makes it very tempting. you should be using another table:

    CREATE TABLE table_a (
        id SERIAL PRIMARY KEY,
        name VARCHAR
    );
    
    CREATE TABLE table_b (
        id SERIAL PRIMARY KEY,
        name VARCHAR
    );
    
    CREATE TABLE a_b (
        a_id INTEGER PRIMARY KEY REFERENCES table_a(id),
        b_id INTEGER PRIMARY KEY REFERENCES table_b(id)
    )
    

    Which is mapped:

    from sqlalchemy import *
    from sqlalchemy.dialects import postgresql
    from sqlalchemy.ext.declarative import declarative_base
    from sqlalchemy.orm import *
    
    Base = declarative_base()
    
    a_b_table = Table("a_b", Base.metadata,
        Column("a_id", Integer, ForeignKey("table_a.id"), primary_key=True),
        Column("b_id", Integer, ForeignKey("table_b.id"), primary_key=True))
    
    class A(Base):
        __tablename__ = "table_a"
        id = Column(Integer, primary_key=True)
        name = Column(String)
    
    class B(Base):
        __tablename__ = "table_b"
        id = Column(Integer, primary_key=True)
        name = Column(String)
        a_set = relationship(A, secondary=a_b_table, backref="b_set")
    

    example:

    >>> print Query(A).filter(A.b_set.any(B.name == "foo"))
    SELECT table_a.id AS table_a_id, table_a.name AS table_a_name 
    FROM table_a 
    WHERE EXISTS (SELECT 1 
    FROM a_b, table_b 
    WHERE table_a.id = a_b.a_id AND table_b.id = a_b.b_id AND table_b.name = :name_1)
    

    If you are stuck with the ARRAY column, your best bet is to use an alternate selectable that “looks” like a proper association table.

    from sqlalchemy import *
    from sqlalchemy.dialects import postgresql
    from sqlalchemy.ext.declarative import declarative_base
    from sqlalchemy.orm import *
    
    Base = declarative_base()
    
    
    class A(Base):
        __tablename__ = "table_a"
        id = Column(Integer, primary_key=True)
        name = Column(String)
    
    class B(Base):
        __tablename__ = "table_b"
        id = Column(Integer, primary_key=True)
        name = Column(String)
        array_a = Column(postgresql.ARRAY(Integer))
    
    a_b_selectable = select([func.unnest(B.array_a).label("a_id"),
                             B.id.label("b_id")]).alias()
    
    A.b_set = relationship(B, secondary=a_b_selectable,
                              primaryjoin=A.id == a_b_selectable.c.a_id,
                              secondaryjoin=a_b_selectable.c.b_id == B.id,
                              viewonly=True,)
    
    B.a_set = relationship(A, secondary=a_b_selectable,
                              primaryjoin=A.id == a_b_selectable.c.a_id,
                              secondaryjoin=a_b_selectable.c.b_id == B.id,
                              viewonly=True)
    

    which gives you:

    >>> print Query(A).filter(A.b_set.any(B.name == "foo"))
    SELECT table_a.id AS table_a_id, table_a.name AS table_a_name 
    FROM table_a 
    WHERE EXISTS (SELECT 1 
    FROM (SELECT unnest(table_b.array_a) AS a_id, table_b.id AS b_id 
    FROM table_b) AS anon_1, table_b 
    WHERE table_a.id = anon_1.a_id AND anon_1.b_id = table_b.id AND table_b.name = :name_1)
    

    And obviously, since there’s no real table there, viewonly=True is neccesary and you can’t get the nice, dynamic objecty goodness you would if you had avoided jaywalking.

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

Sidebar

Related Questions

Suppose you have two tables in PostgreSQL. Table A has field x , which
Suppose I have two queries on a database table. The queries are defined in
Suppose I have a table called Companies that has a DepartmentID column. There's also
In PostgreSQL I have a table with a varchar column. The data is supposed
I have a 'users' table with two columns, 'email' and 'new_email'. I need: A
Is it possible to have selective queries in PostgreSQL which select different tables/columns based
I am new to PostgreSQL. Suppose I have a table as under colorname Hexa
Simple question (I hope): I have a simple two column table, a measurement and
Suppose we have a table A: itemid mark 1 5 2 3 and table
Suppose I have a class module clsMyClass with an object as a member variable.

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.