Pydantic: Work around model field called 'schema' with orm mode

Created on 3 Jan 2020  路  3Comments  路  Source: samuelcolvin/pydantic

Question

I have a model which has a field called schema. In order to work around I have tried setting an alias. However, when using ORM mode... I get this error:

TypeError: 'dataset_schema' is an invalid keyword argument for Dataset

The sqlalchemy model field is called schema

Is there a way to work around this? Thanks!

python 3.7.5
pydantic 1.3

class SchemaSpecBase(BaseModel):
    dataset_schema: Dict

    class Config(BaseConfig):
        allow_population_by_alias = True
        fields = {'dataset_schema': {'alias': 'schema'}}


class SchemaSpecInDB(BaseMixinResource, SchemaSpecBase):
    dataset: int
    schema_version = int
    schema_type = SchemaType

    class Config:
        orm_mode = True
question

Most helpful comment

ah ha, needs to be schemaspec = SchemaSpecBase.dict(by_alias=True). Excellent. Thank you for the help!

All 3 comments

allow_population_by_alias changed to allow_population_by_field_name in v1.

I suspect the problem is BaseMixinResource which is stopping inheritance of Config from SchemaSpecBase, but hard to know.

Does it work if you just use SchemaSpecBase with orm_mode = True added to it's config?

Updated allow_population_by_field_name.

Using SchemaSpecBase with orm_mode = True yields the same result.

Just using SchemaSpecBase directly... should using it this way produce an aliased field?

schemaspec = SchemaSpecBase.dict()

ah ha, needs to be schemaspec = SchemaSpecBase.dict(by_alias=True). Excellent. Thank you for the help!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nav picture nav  路  3Comments

sbv-trueenergy picture sbv-trueenergy  路  3Comments

samuelcolvin picture samuelcolvin  路  3Comments

ashpreetbedi picture ashpreetbedi  路  3Comments

engstrom picture engstrom  路  3Comments