问答详情
源自:2-6 Pydantic 基础教程

一毛一样的代码为什么我报错

# _*_coding:utf-8_*_
from datetime import datetime, date
from typing import List, Optional
from sqlalchemy import Column, Integer, String,ARRAY
from sqlalchemy.ext.declarative import declarative_base

from pydantic import BaseModel, constr

Base = declarative_base()


class CompanyOrm(Base):
    __tablename__ = 'companies'
    id = Column(Integer, primary_key=True, nullable=False),
    public_key = Column(String(20), index=True, nullable=False,unique=True)
    name = Column(String(50), unique=True)
    domains = Column(ARRAY(String(100)))


class CompanyModel(BaseModel):
   
    public_key: constr(max_length=20)
    name: constr(max_length=50)
    domains: List[constr(max_length=100)]

    class Config:
        orm_mode = True


co_orm = CompanyOrm(
    id=123,
    public_key='foobar',
    name='Testing',
    domains=['example.com', 'foobar.com'],
)

print(CompanyModel.from_orm(co_orm))

http://img.mukewang.com/6363550e0001b7ba15920615.jpg

提问者:yu444913387 2022-11-03 13:43

个回答

  • 慕移动9047805
    2022-11-15 11:38:26

    你的 CompanyModel 没有定义 id 字段