Skip to content

Specify 'extend_existing=True' to redefine options and columns on an existing Table object. #368

@wanghaisheng

Description

@wanghaisheng

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 datetime import datetime
from typing import Optional, List

import sqlmodel
from fastapi_amis_admin.amis.components import InputRichText, InputImage, ColumnImage
from fastapi_amis_admin.models.enums import IntegerChoices
from fastapi_amis_admin.models.fields import Field
from fastapi_user_auth.auth.models import User
from sqlalchemy import Column, String


class VideoStatus(IntegerChoices):
    unpublished = 0, '未发布'
    published = 1, '已发布'
    inspection = 2, '审核中'
    disabled = 3, '已禁用'


# Create your models here.

class BaseSQLModel(sqlmodel.SQLModel):
    id: int = Field(default=None, primary_key=True, nullable=False)

    class Config:
        use_enum_values = True




class VideoPlatformLink(sqlmodel.SQLModel, table=True):
    __tablename__ = 'publish_video_platforms'
    platform_id: Optional[int] = Field(
        default=None, foreign_key="publish_platform.id", primary_key=True
    )
    video_id: Optional[int] = Field(
        default=None, foreign_key="publish_video.id", primary_key=True
    )

class VideoChannelLink(sqlmodel.SQLModel, table=True):
    __tablename__ = 'publish_video_channels'
    channel_id: Optional[int] = Field(
        default=None, foreign_key="publish_channel.id", primary_key=True
    )
    video_id: Optional[int] = Field(
        default=None, foreign_key="publish_video.id", primary_key=True
    )


class Platform(BaseSQLModel, table=True):
    __tablename__ = 'publish_platform'
    name: str = Field(..., title='PlatformName', sa_column=Column(String(255), unique=True, index=True, nullable=False))
    videos: List["Video"] = sqlmodel.Relationship(back_populates="platforms",link_model=VideoPlatformLink)
    channels: List["Channel"] = sqlmodel.Relationship(back_populates="platform")


class Channel(BaseSQLModel, table=True):
    __tablename__ = 'publish_channel'
    extend_existing=True
    name: str = Field(title='ChannelName', sa_column=Column(String(100), unique=True, index=True, nullable=False))
    description: str = Field(default='', title='Description', amis_form_item='textarea')
    status: bool = Field(None, title='status')
    videos: List["Video"] = sqlmodel.Relationship(back_populates="channels",link_model=VideoChannelLink)
    platform: Optional[Platform] = sqlmodel.Relationship(back_populates="channels")



class Video(BaseSQLModel, table=True):
    __tablename__ = 'publish_video'
    title: str = Field(title='VideoTitle', max_length=200)
    img: str = Field(None, title='VideoImage', max_length=300,
                     amis_form_item=InputImage(maxLength=1, maxSize=2 * 1024 * 1024,
                                               receiver='post:/admin/file/upload'),
                     amis_table_column=ColumnImage(width=100, height=60, enlargeAble=True))
    description: str = Field(default='', title='VideoDescription', amis_form_item='textarea')
    status: VideoStatus = Field(VideoStatus.unpublished, title='status')
    content: str = Field(..., title='VideoContent', amis_form_item=InputRichText())
    create_time: Optional[datetime] = Field(default_factory=datetime.utcnow, title='CreateTime')
    source: str = Field(default='', title='VideoSource', max_length=200)

    # channel_id: Optional[int] = Field(default=None, foreign_key="publish_channel.id", title='ChannelId')
    channels: List[Channel] = sqlmodel.Relationship(back_populates="videos",link_model=VideoChannelLink)

    platforms: List[Platform] = sqlmodel.Relationship(back_populates="videos", link_model=VideoPlatformLink)

    user_id: int = Field(default=None, foreign_key="auth_user.id", title='UserId')
    user: User = sqlmodel.Relationship()

Description

I want to achieve

  1. one Platform has many videos ,one video has many platform
    so VideoPlatformLink to many-many link
  2. one Platform has many channel,one channel has one platform
    so i use
    channels: List["Channel"] = sqlmodel.Relationship(back_populates="platform")

3.one channel has many videos, one video has many channels
so VideoChannelLink to many2many

it tell me

sqlalchemy.exc.InvalidRequestError: Table 'publish_channel' is already defined for this MetaData instance.  Specify 'extend_existing=True' to redefine options and columns on an existing Table object.

Operating System

Windows

Operating System Details

No response

SQLModel Version

0.0.6

Python Version

3.8.0

Additional Context

I have add ** extend_existing=True**
but it still tell me

sqlalchemy.exc.InvalidRequestError: Table 'publish_channel' is already defined for this MetaData instance.  Specify 'extend_existing=True' to redefine options and columns on an existing Table object.

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