Skip to content

Conversation

Pritish053
Copy link

@Pritish053 Pritish053 commented Jul 19, 2025

Fix PostgreSQL schema creation for non-default schemas

Summary

Fixes #1671 by implementing automatic PostgreSQL schema creation and schema-qualified SQL generation.

Previously, Tortoise ORM would fail when trying to generate tables in non-default PostgreSQL schemas because:

  1. Schemas weren't automatically created
  2. Table references weren't schema-qualified

This PR resolves both issues, enabling seamless use of custom PostgreSQL schemas.

Key Changes

Core Implementation

  • Automatic Schema Creation: Added CREATE SCHEMA IF NOT EXISTS generation for PostgreSQL
  • Schema-Qualified References: All SQL now uses proper schema qualification ("schema"."table")
  • Foreign Key Support: FK references work correctly across schemas
  • M2M Table Support: Many-to-many tables respect schema settings

Files Modified

  • tortoise/backends/base/schema_generator.py - Added schema qualification methods
  • tortoise/backends/base_postgres/schema_generator.py - PostgreSQL schema creation logic
  • tests/schema/test_schema_creation.py - Comprehensive test coverage
  • CHANGELOG.rst - Added entry for the fix

Before vs After

Before (fails):

class User(Model):
    name = fields.CharField(max_length=50)
    class Meta:
        schema = "pgdev"

# Tortoise.generate_schemas() would fail:
# relation "pgdev.users" does not exist

After (works):

CREATE SCHEMA IF NOT EXISTS "pgdev";
CREATE TABLE IF NOT EXISTS "pgdev"."users" (
    "id" BIGSERIAL NOT NULL PRIMARY KEY,
    "name" VARCHAR(50) NOT NULL
);

Testing

  • ✅ New test suite validates schema SQL generation
  • ✅ Live PostgreSQL testing confirms end-to-end functionality
  • ✅ All existing tests continue to pass
  • ✅ Backward compatibility verified

Checklist

  • [+] My code follows the code style of this project
  • [+] My change requires a change to the documentation
  • [+] I have updated the documentation accordingly
  • [+] I have added the changelog accordingly
  • [+] I have read the CONTRIBUTING document
  • [+] I have added tests to cover my changes
  • [+] All new and existing tests passed

Impact: Fully backward compatible - no breaking changes for existing code.

Resolves issue tortoise#1671 where Tortoise ORM failed to generate tables
for non-default schemas.

Changes:
- Modified BaseSchemaGenerator to use schema-qualified table names
  in CREATE TABLE statements when schema is specified
- Added automatic CREATE SCHEMA generation for PostgreSQL backends
- Updated template strings to properly handle qualified table names
- Fixed M2M table creation to work with schemas
- Added tests for schema creation functionality
@Pritish053 Pritish053 force-pushed the fix-postgresql-schema-creation branch from cba5f4c to 5c35bc7 Compare July 20, 2025 00:07
@Pritish053
Copy link
Author

Can anyone approve this for the workflow ? @henadzit

Copy link

codspeed-hq bot commented Jul 22, 2025

CodSpeed Performance Report

Merging #1979 will not alter performance

Comparing Pritish053:fix-postgresql-schema-creation (5c35bc7) with develop (65bc23e)

Summary

✅ 16 untouched benchmarks

Copy link
Contributor

@henadzit henadzit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the contribution!

Looks like your IDE reformatted a few file, and this makes it hard to review the PR. Can you please rollback the formatting changes?

- Add `no_key` parameter to `queryset.select_for_update`.
- `F()` supports referencing JSONField attributes, e.g. `F("json_field__custom_field__nested_id")` (#1960)

Fixed
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0.25.1 has been already released, you need to add 0.25.2 (unreleased) and put your changes there.

# Get qualified table name for FK reference if related model has schema
qualified_related_table_name = self._get_qualified_table_name(
reference.related_model
).strip('"')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would you remove the leading and trailing " but leave them in the middle around .?

@aver-develop
Copy link

Hi! What the progress on this MR?

@Pritish053
Copy link
Author

Hi! What the progress on this MR?

Will pick this up on the weekend

@Pritish053
Copy link
Author

Thank you for the contribution!

Looks like your IDE reformatted a few file, and this makes it hard to review the PR. Can you please rollback the formatting changes?

Sure

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Tortoise ORM fails to generate tables for non-default schemas
3 participants