2

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?

SimonZen
  • 131
  • 1
  • 11
  • What is `MainMetaCreate` ? and how do you create your new obj in the db ? – fchancel May 14 '22 at 16:54
  • found the answer in here https://stackoverflow.com/questions/64414030/how-to-use-nested-pydantic-models-for-sqlalchemy-in-a-flexible-way – SimonZen May 14 '22 at 21:28
  • 1
    Does this answer your question? [How to use nested pydantic models for sqlalchemy in a flexible way](https://stackoverflow.com/questions/64414030/how-to-use-nested-pydantic-models-for-sqlalchemy-in-a-flexible-way) – snakecharmerb Aug 25 '22 at 09:07

0 Answers0