'AttributeError: 'BaseModel' object has no attribute '_sa_instance_state'

I have the following code in my app:

models.py

class MainTable(Base):
    __tablename__ = "main_table"
    id = Column(Integer, primary_key=True, index=True)
    name = Column(String, unique=True, index=True, nullable=False)
    owner = Column(String, index=True)

    metas = relationship("ChildTable", back_populates="item")

class ChildTable(Base):
    __tablename__ = "child"
    id = Column(Integer, primary_key=True, index=True)
    segment = Column(Integer)
    name = Column(String)
    main_name = Column(String, ForeignKey('main_table.name'), nullable=False)
    item = relationship("MainTable", back_populates="metas")
    __table_args__ = (UniqueConstraint('segment', 'main_name', name='_meta_for_main'),
                      )

And when I run my app, and create new obj in db:

{
  "name": "string",
  "owner": "string",
  "metas": [
     {
      "segment": 0,
      "name": "string",
      "main_name": "string",
    }
   ]
}

I get the following error:

AttributeError: 'MainMetaCreate' object has no attribute '_sa_instance_state'

schemas.py

class EnvironmentMetaBase(BaseModel):
    segment: int
    name: str
    main_name: str

Haw you met the following problem?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source