Sling Academy
Home/SQLAlchemy/Using multiple foreign keys in SQLAlchemy

Using multiple foreign keys in SQLAlchemy

Last updated: January 03, 2024

Introduction

SQLAlchemy provides a powerful ORM for Python developers to simplify database interactions. In this tutorial, we will explore the use of multiple foreign keys in a single table, deep-diving into relationships and join conditions in SQLAlchemy.

Understanding Foreign Keys

Before diving into multiple foreign keys, it’s crucial to understand the purpose of a foreign key. A foreign key is a field in a relational database table that creates a link between two tables. This link ensures referential integrity by enforcing that the value in the foreign key column corresponds to a value in the primary key of another table.

Basic Usage of Foreign Keys

from sqlalchemy import create_engine, Column, Integer, String, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, relationship

Base = declarative_base()

# Define User table
class User(Base):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True)
    name = Column(String)

# Define Post table
class Post(Base):
    __tablename__ = 'posts'
    id = Column(Integer, primary_key=True)
    author_id = Column(Integer, ForeignKey('users.id'))
    title = Column(String)

    # Define relationship
    author = relationship('User', back_populates='posts')

User.posts = relationship('Post', back_populates='author')

# Initialize DB connection
engine = create_engine('sqlite:///mydatabase.db')
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()

Multiple Foreign Keys to the Same Table

There are scenarios where a single table might have several foreign keys referencing the same related table. For example, in an ordering system, an order detail might have separate foreign keys linking to a ‘created_by’ user and an ‘approved_by’ user.

# Define Order table
class Order(Base):
    __tablename__ = 'orders'
    id = Column(Integer, primary_key=True)
    created_by_id = Column(Integer, ForeignKey('users.id'))
    approved_by_id = Column(Integer, ForeignKey('users.id'))

    # Define relationships
    created_by = relationship('User', foreign_keys=[created_by_id])
    approved_by = relationship('User', foreign_keys=[approved_by_id])

Advanced Relationship Patterns

SQLAlchemy offers advanced patterns, such as polymorphic models, to establish complex relationships. Complex arrangements with multiple foreign keys often require explicit join conditions and may involve secondary tables for many-to-many relationships.

Conclusion

In summary, effectively using multiple foreign keys in SQLAlchemy demands a clear understanding of relational table structures and the relationships between them. As we have demonstrated, SQLAlchemy facilitates these complexities, making it a robust tool for developers. Grasping these concepts paves the way for designing intricate database schemas that power complex applications.

Next Article: LEFT OUTER JOIN in SQLAlchemy

Previous Article: How to Save CSV Data in SQLAlchemy

Series: SQLAlchemy Tutorials: From Basic to Advanced

SQLAlchemy

You May Also Like

  • SQLAlchemy: Counting rows for each category (2 approaches)
  • SQLAlchemy: Adding a calculated column to SELECT query
  • SQLAlchemy: Grouping data on multiple columns
  • SQLAlchemy: How to temporarily delete a row
  • SQLAlchemy Composite Indexes: The Complete Guide
  • Full-Text Search in SQLAlchemy: The Ultimate Guide
  • SQLAlchemy: What if you don’t close database connections?
  • SQLAlchemy: How to Remove FOREIGN KEY Constraints (2 Ways)
  • SQLAlchemy: How to Create and Use Temporary Tables
  • SQLAlchemy: Saving Categories and Subcategories in the Same Table
  • SQLAlchemy: How to Automatically Delete Old Records
  • Weighted Random Selection in SQLAlchemy: An In-Depth Guide
  • SQLAlchemy: created_at and updated_at columns
  • How to Use Regular Expressions in SQLAlchemy
  • SQLAlchemy: Ways to Find Results by a Keyword
  • SQLAlchemy: How to Connect to MySQL Database
  • SQLAlchemy: How to Bulk Insert Data into a Table
  • SQLAlchemy: How to Update a Record by ID
  • SQLAlchemy: Singular vs Plural Table Names