Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ordering output doesn't work for auto_field #306

Closed
KwonL opened this issue Apr 24, 2020 · 3 comments · Fixed by #307
Closed

Ordering output doesn't work for auto_field #306

KwonL opened this issue Apr 24, 2020 · 3 comments · Fixed by #307

Comments

@KwonL
Copy link

KwonL commented Apr 24, 2020

In marshmallow, to order serialization output, set ordered = True in the Meta class as follows.

class MyModelSchema(SQLAlchemyAutoSchema):
    class Meta:
        model = MyModel
        ordered = True

    some_field = auto_field(validate=validate.Range(min=0, max=10))

It works well without auto_field, but it generate following error with auto_field.

AttributeError: 'SQLAlchemyAutoField' object has no attribute '_creation_index'

Is there any solution for this?

@peterschutt
Copy link
Contributor

I think I have worked out a fix, but I'm not a maintainer so would have to wait until someone is able to pay the issue some attention.

Say with this model:

class MyModel(Base):
    __tablename__ = "mymodel"
    id = Column(Integer, primary_key=True, autoincrement=True)
    some_field = Column(Integer, nullable=False)

and this schema (no auto_field()):

class MyModelSchema(SQLAlchemyAutoSchema):
    class Meta:
        model = MyModel
        ordered = True

... an instance serialized to: OrderedDict([('id', 1), ('some_field', 111)]).

And with this schema (with auto_field()):

class MyModelSchema(SQLAlchemyAutoSchema):
    class Meta:
        model = MyModel
        ordered = True

    some_field = auto_field()

... the same instance serialized to: OrderedDict([('some_field', 111), ('id', 1)]).

Is it confusing, expected, or doesn't matter that the declared field is ordered before the auto generated field?

@KwonL
Copy link
Author

KwonL commented Apr 25, 2020

Is it confusing, expected, or doesn't matter that the declared field is ordered before the auto generated field?

If we can get consistent result, I think order of field doesn't matter.
It makes sense that auto_field is ordered before generated field because we explicitly declared that field.

@sloria
Copy link
Member

sloria commented Apr 26, 2020

Release in 0.23.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants