How to specify a different name for the db table that a SQLModel refers to?
See original GitHub issueFirst 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 SQLModel, Field
# If the actual db table is called `assets`, for example, how can I keep the following model named `Product` but have it refer to the correct table?
class Product(SQLModel, table=True):
id: Optional[int] = Field(None, primary_key=True)
name: str
Description
- I have an existing database with table names that are different than what I would like my model names to be.
- The documentation here under the “SQL Table Names” section notes that there will be information on how to do this in the “Advanced Guide,” but that section has not been worked on yet…
Operating System
Linux, macOS
Operating System Details
No response
SQLModel Version
0.0.4
Python Version
3.10.0
Additional Context
No response
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:11 (2 by maintainers)
Top Results From Across the Web
Create a Table with SQLModel - Use the Engine
Define a table with SQLModel; Create the same SQLite database and table ... The name of each of these variables will be the...
Read more >SQLModel
SQLModel, SQL databases in Python, designed for simplicity, compatibility, ... represent tables and rows, and that way communicate with the SQL database.
Read more >Create Connected Tables - SQLModel
So, the first step is to create more than one table and connect them, so that each row in one table can reference...
Read more >Read Data - SELECT - SQLModel
For example, in our database, we only have one table that has all the columns, id , name , secret_name , age ....
Read more >Read Connected Data - SQLModel
It means, more or less: Hey SQL database , please go and SELECT some data for me. I'll first tell you the columns...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
If you’re using Pylance/Pyright, you should do
If you don’t include the
: strannotation, you’ll see the errorCredit to @indivisible for https://github.com/tiangolo/sqlmodel/issues/98#issuecomment-996651619
Seems that setting
__tablename__as part of the class definition worked (at least, it seems to have created the table with the correct name when testing with sqlite):I tried this based on reading this in the sqlalchemy docs: https://docs.sqlalchemy.org/en/13/orm/extensions/declarative/table_config.html
Just to confirm though: is this the recommended way to deal with the scenario?