How to specify a different name for the db table that a SQLModel refers to?

See original GitHub issue

First 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:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
maresbcommented, Dec 18, 2021

If you’re using Pylance/Pyright, you should do

__tablename__: str = name_in_database

If you don’t include the : str annotation, you’ll see the error

Expression of type “str” cannot be assigned to declared type “declared_attr” “str” is incompatible with “declared_attr

Credit to @indivisible for https://github.com/tiangolo/sqlmodel/issues/98#issuecomment-996651619

4reactions
taranlu-houzzcommented, Nov 19, 2021

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):

class Product(SQLModel, table=True):

    __tablename__ = "assets"

    id: Optional[int] = Field(None, primary_key=True)
    name: str

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?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found