Skip to content

Setting PostgreSQL schema  #20

@Chunkford

Description

@Chunkford

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the SQLModel documentation, with the integrated search.
  • I already searched in Google "How to X in SQLModel" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to SQLModel but to Pydantic.
  • I already checked if it is not related to SQLModel but to SQLAlchemy.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

from datetime import datetime
from typing import Optional, List
from sqlmodel import Field, SQLModel


class Campaign(SQLModel, table=True):
    campaign_id: Optional[int] = Field(
        default=None,
        primary_key=True,
        schema_extra={'schema': 'test'},
    )
    created_at: Optional[datetime] = Field(
        default=None,
        schema_extra={'schema': 'test'}
    )
    updated_at: Optional[datetime] = Field(
        default=None,
        schema_extra={'schema': 'test'}
    )
    name: str = Field(
        schema_extra={'schema': 'test'}
    )
    description: Optional[str]= Field(
        default=None,
        schema_extra={'schema': 'test'}
    )
    template_id: int = Field(
        schema_extra={'schema': 'test'}
    )
    orders_needed: int = Field(
        schema_extra={'schema': 'test'}
    )
    postcode_areas: List[str] = Field(
        schema_extra={'schema': 'test'}
    )

Description

I've created a model to import and I'm trying to connect to PostgreSQL to an already created database.
But I have my tables in a different schema called test and not public.
After looking through the code I was hoping the input schema_extra would do the trick

https://github.com/tiangolo/sqlmodel/blob/02da85c9ec39b0ebb7ff91de3045e0aea28dc998/sqlmodel/main.py#L158

But, I must be wrong.

This is the SQL being outputted:

SELECT campaign.campaign_id, campaign.created_at, campaign.updated_at, campaign.name, campaign.description, campaign.template_id, campaign.orders_needed, campaign.postcode_areas 
FROM campaign

Ideally I want it to say:

SELECT campaign.campaign_id, campaign.created_at, campaign.updated_at, campaign.title, campaign.description, campaign.template_id, campaign.orders_needed, campaign.postcode_areas 
FROM test.campaign AS campaign

Is there a way to map a model to a different schema other than the default public schema?

Operating System

Linux

Operating System Details

No response

SQLModel Version

0.0.3

Python Version

3.8.10

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions