Repository navigation
Creating column with an alias name #93
Description
Activity
- addedquestionFurther information is requestedFurther information is requested
on Sep 14, 2021 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
CommonCrawlertable will actually be used ascommoncrawlerwith 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
schemascolumn declaration:schemas: Optional[str] = Field(default=None, sa_column_kwargs={'name': 'schema'})This will allow you to use
CommonCrawler.schemasin your Python code, but the column in the table itself will have the nameschema. I don't think it's documented anywhere yet, but it looks like you can pass SQLAlchemyColumn.__init__()keyword arguments via adictin theFieldconstructor keyword argumentsa_column_kwargs.In case you actually wanna use the table name
CommonCrawler(and notcommoncrawler), 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'} )
Reacted by Daniel Thaagaard AndreasenThank you @rodriguez-bytes
I ended up solving the problem with a sqlalchemy Base classclass 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}'>"- locked and limited conversation to collaborators
on Aug 26, 2025
First Check
Commit to Help
Example Code
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