Giter Site home page Giter Site logo

Comments (5)

danjac avatar danjac commented on July 28, 2024

It appears this issue is not just with polymorphic types, but with trying to save any ChoiceType column.

class User(Base):

    __tablename__ = 'users'

    TYPES = [
        (u'customer', 'Customer'),
        (u'admin', 'Admin'),
   ]

    id = Column(Integer, primary_key=True)
    type = Column(ChoiceType(TYPES), default='customer')
    email = Column(String(180), unique=True)
    is_active = Column(Boolean, default=True)

 session.add(User(type=u'admin', email='test')
 session.commit()

File "path/sqlalchemy-utils/sqlalchemy_utils/types/choice.py", line 107, in process_bind_param
return value.code
sqlalchemy.exc.StatementError: 'unicode' object has no attribute 'code' (original cause: AttributeError: 'unicode' object has no attribute 'code') u'INSERT INTO users (type, email, password,is_active) VALUES (?, ?, ?, ?, ?, ?, ?)' [{'type': u'admin', 'email': 'test'}]

So it appears the ChoiceType is completely broken, unless I'm missing something. The code should be something like:

def process_bind_param(self, value, dialect):
    if isinstance(value, Choice):
        return value.code
    return value

from sqlalchemy-utils.

kvesteri avatar kvesteri commented on July 28, 2024

It seems you have not setup coercion_listener properly. See: http://sqlalchemy-utils.readthedocs.org/en/latest/coercion.html

from sqlalchemy-utils.

danjac avatar danjac commented on July 28, 2024

Tried with the coercion_listener, same error:

listen(mapper, 'mapper_configured', coercion_listener)

from sqlalchemy-utils.

kvesteri avatar kvesteri commented on July 28, 2024

DId you put the listener before the model definition? Clearly the problem is the unicode value does not get coerced to Choice object.

There is also a test case for this (which passes). So the problem must be with the data coercion. See: See: https://github.com/kvesteri/sqlalchemy-utils/blob/master/tests/types/test_choice.py#L39-L48

If you can reproduce the problem as a test case, that would be very helpful.

from sqlalchemy-utils.

danjac avatar danjac commented on July 28, 2024

Yes, tried placing the call before and after the model definition. It could however be an issue with the import of a base class in another module (i.e it's called after the base class is defined), so I'll investigate that possibility.

from sqlalchemy-utils.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.