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

Serialization with Mixins #96

Open
digitalkaoz opened this issue Nov 6, 2020 · 3 comments
Open

Serialization with Mixins #96

digitalkaoz opened this issue Nov 6, 2020 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@digitalkaoz
Copy link
Contributor

Currently it seems serialization with attributes defined in mixins wont work:

# mixin
from sqlalchemy import BigInteger
from sqlathanor import Column


class PrimaryMixin(object):
    """
    Mixin for adding a autoincrementing PK
    """

    id: int = Column(BigInteger, primary_key=True, autoincrement=True, supports_dict=True)
#concrete class
from sqlalchemy import UnicodeText, BigInteger, ForeignKey
from sqlathanor import Column, relationship

from model import deferred_reflection, base
from model.mixins.primary import PrimaryMixin


class Example(PrimaryMixin, base, deferred_reflection):
    """
    Example Table
    """
    __tablename__ = "example"

    name: str = Column(UnicodeText, comment="name", supports_dict=True)
#test case
class ExampleTestCase(unittest.TestCase):
    def test_serialize_to_dict(self):
        m = Example(name="Test", id=4711)

        self.assertEqual(m.to_dict(), {
            'id': 4711,
            'name': 'Test'
        })

what am i doing wrong here? The id is correctly filled and setup by SQLAlchemy itself, but the serialization definition isnt respected.
i just followed the working/common https://docs.sqlalchemy.org/en/13/orm/extensions/declarative/mixins.html#mixing-in-columns to keep my models dry

@insightindustry insightindustry self-assigned this Nov 6, 2020
@insightindustry insightindustry added the bug Something isn't working label Nov 6, 2020
@insightindustry
Copy link
Owner

Sorry you're running into this, @digitalkaoz ! Thanks for providing the example code and the test case - I'll run some tests and see what's happening here. This should work properly, I would expect, so it's likely a bug somewhere causing an issue. With luck, I'll be able to debug it over this weekend and if necessary do a new release with a bug fix.

@digitalkaoz
Copy link
Contributor Author

Yeah, No worries! Thanks for your awesome piece of software :) i tried all kinds of (de)serialization libraries and found yours the best. It could be a little more performant in large entities / list, but maybe i have time to dig into this

@digitalkaoz
Copy link
Contributor Author

digitalkaoz commented Nov 10, 2020

@insightindustry it works like this:

class PrimaryMixin(object):

    @declared_attr
    def id(cls):
        return Column(BigInteger, primary_key=True, autoincrement=True, **serialize)

but the original approach seems a bit clearer to me, so i still suggest its a bug

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants