How do I rename my SQLModel class from table name "AppUser" to "app_user" by not changing Class name? #604
-
First Check
Commit to Help
Example Codefrom datetime import datetime
from typing import Optional
from pydantic import EmailStr, ValidationError
from sqlmodel import Field, Session, SQLModel, create_engine, select
class AppUserModel(SQLModel, table=True): # this class, I need to rename it to "app_user" when generated into mysql
id: Optional[int] = Field(default=None, primary_key=True)
openid: str = Field(default=None, index=True)
pkg_name: str = Field(default=None, index=True)
unionid: str = Field(default=None, nullable=True, index=True)
platform: str = Field(default='weapp', index=True)
username : str = None
password : str = None
email : Optional[EmailStr] = None
phone : str = None
realname : str = None
avatar : str = None
user_desc : str = None
is_enabled : bool = True
login_time : int = None
session_key: str = None
created_at: datetime = Field(default=datetime.utcnow())
updated_at: datetime = Field(default_factory=datetime.utcnow) DescriptionWiki was wrote it about rename table name in advance, but no way to rename Operating SystemmacOS Operating System DetailsNo response SQLModel Version0.0.8 Python Version3.10 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Set a property class AppUserModel(SQLModel, table=True):
__tablename__ = "app_user"
... |
Beta Was this translation helpful? Give feedback.
-
Same as #159 From main.py
should be
Do not reinvent wheel to convert to snake case. |
Beta Was this translation helpful? Give feedback.
-
This issue is particularly difficult. Here is a minimalist implementation. Code comments highlight shortcomings
Multiple database support is always necessary. Every database will have an For multiple databases to have table, Example class names: Base12345, Base12346, AlembicVersion12345, AlembicVersion12346 |
Beta Was this translation helpful? Give feedback.
Set a property
__tablename__
in your model and set its value toapp_user
.