Skip to content

Problem with the metadata field (NameError: Field name "metadata" shadows a BaseModel attribute; use a different field name with "alias='metadata'".) #290

Open
@MichaelLan

Description

@MichaelLan

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

# database.py

from sqlmodel import Session, create_engine
from core.settings import settings


engine = create_engine(settings.SQLALCHEMY_DATABASE_URL, echo=True)

def get_session():
    with Session(engine) as session:
        yield session


# models.py

from typing import Optional

from sqlmodel import Field, SQLModel


class Companies(SQLModel, table=True):
    avatar: Optional[str] = None
    business_name: Optional[str] = None
    country_id: int = Field(index=True)
    created: str
    crm_id: Optional[str] = None
    id: Optional[str] = Field(default=None, primary_key=True)
    is_active: bool
    is_removed: bool
    metadata_: Optional[str] = Field(default=None, alias="metadata")  # Error: Companies.metadata_ does not exist
    
    # before
    metadata: Optional[str] = Field(default=None)  # Error: NameError

Description

I created a model but this have a field named metadata, is an existing database, so I can't change the fields names, since the SQLModel class also has a field called metadata, when I run the app I get the following error
NameError: Field name "metadata" shadows a BaseModel attribute; use a different field name with "alias='metadata'".

I used the Field class of sqlmodel and assigned the alias but it does not work.
metadata_: Optional[str] = Field(default=None, alias="metadata")
With this the error is that Table.metadata_ field does not exist

Is there any way to create an tabla with the metadata field without giving problems with SQLModel?

Operating System

Linux

Operating System Details

No response

SQLModel Version

0.0.6

Python Version

Python 3.9.5

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions