Skip to content

Creating column with an alias name #93

Description

@gideonmandu

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 typing import Optional
from sqlmodel import Field, SQLModel


class CommonCrawler(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    domain: str
    country: Optional[str] = Field(default=None)
    whois: Optional[str] = Field(default=None)
    schemas: Optional[str] = Field(
        alias="schema", default=None, title="schema"
    )
    phone_number: Optional[str] = Field(default=None)
    city: Optional[str] = Field(default=None)

    class Config:
        title = 'common_crawler'

    def __repr__(self):
        return f"Domain(id={self.id!r}, domain={self.domain!r}, country={self.country!r})"

Description

I Tried creating a Table with schema as one of the columns but got an error NameError: Field name "schema" shadows a BaseModel attribute; use a different field name with "alias='schema'".
Error message adviced I use an alias and a diffrent column name.
I changed the name to schemas but its alias is not being applied on table creation.
I am using sqlite database for this specific use case.

Operating System

Linux

Operating System Details

Parrot security linux distro

SQLModel Version

0.0.4

Python Version

3.9.2

Additional Context

No response

Activity

  1. rodriguez-bytes commented on Sep 26, 2021

    @rodriguez-bytes

    I also had trouble with this. My specific use case was trying to make SQLModel classes for an existing SQL Server host that has:

    • Multiple databases
    • Tables using title case (i.e. your CommonCrawler table will actually be used as commoncrawler with SQLModel; might be fine if you don't need to interface with existing databases, but this was an issue with my project as the table was not accessible with its lowercase name)
    • Schema prefixes used in the database tables
    • Column names that use title case (SQLModel will always use lowercase column names, even if you define them in title case, notwithstanding the pain you feel in your heart for not following snake case conventions)

    How I figured out that last point above will most likely help you out. Use the following for your schemas column declaration:

    schemas: Optional[str] = Field(default=None, sa_column_kwargs={'name': 'schema'})

    This will allow you to use CommonCrawler.schemas in your Python code, but the column in the table itself will have the name schema. I don't think it's documented anywhere yet, but it looks like you can pass SQLAlchemy Column.__init__() keyword arguments via a dict in the Field constructor keyword argument sa_column_kwargs.

    In case you actually wanna use the table name CommonCrawler (and not commoncrawler), add __tablename__='CommonCrawler' as a class attribute, i.e.

    class CommonCrawler(SQLModel, table=True):
        __tablename__ = 'CommonCrawler'
        
        # In case you have schema prefixes in your table name, e.g. `sch`
        # Ignore if you don't have that requirement
        __table_args__ = {'schema': 'sch'} 
    
        # Skipping your other column declarations
        schemas: Optional[str] = Field(
            default=None, 
            sa_column_kwargs={'name': 'schema'}
        )
  2. gideonmandu commented on Sep 26, 2021

    @gideonmandu
    Author

    Thank you @rodriguez-bytes
    I ended up solving the problem with a sqlalchemy Base class

    class Domain(Base): __tablename__ = 'common_crawler' id = Column(Integer, primary_key=True) domain = Column(String) country = Column(String) whois = Column(String) schema = Column(String) phone_number = Column(String) city = Column(String) def __repr__(self): return f"<Domain(domain='{self.domain!r}', country='{self.country!r}', whois='{self.whois!r}', schema='{self.schema!r}', phone_number='{self.phone_number!r}', city='{self.city!r}'>"

  3. locked and limited conversation to collaborators on Aug 26, 2025
  4. converted this issue into a discussion #1535 on Aug 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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