Creating column with an alias name #1535
-
First Check
Commit to Help
Example Codefrom 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})" DescriptionI 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'". Operating SystemLinux Operating System DetailsParrot security linux distro SQLModel Version0.0.4 Python Version3.9.2 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
I also had trouble with this. My specific use case was trying to make SQLModel classes for an existing SQL Server host that has:
How I figured out that last point above will most likely help you out. Use the following for your
This will allow you to use In case you actually wanna use the table name 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'}
) |
Beta Was this translation helpful? Give feedback.
-
Thank you @rodriguez-bytes
|
Beta Was this translation helpful? Give feedback.
I also had trouble with this. My specific use case was trying to make SQLModel classes for an existing SQL Server host that has:
CommonCrawler
table will actually be used ascommoncrawler
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)How I figured out…