You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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:
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.
classCommonCrawler(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 declarationsschemas: Optional[str] =Field(
default=None,
sa_column_kwargs={'name': 'schema'}
)
Activity
rodriguez-bytes commentedon Sep 26, 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:
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)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.gideonmandu commentedon Sep 26, 2021
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}'>"