Update: I fixed the symptom by changing the column datatypes back to 
db.String and casting when a UnicodeDecodError is raised. 

db.session.query(cast(Annotations.annotation, sa.Unicode)


However, my feeling is that this is not a great situation as it looks like 
I have two different string encodings (?) in the same table-field.  This is 
because I was running into an issue where long strings passed as query 
parameters to _.in() or `==` filters were raising ProgrammingError exceptions 
about varchar(max) and text types not being comparable.  To work around, I 
trapped the exceptions and casted the parameters as db.String in the filter

.filter(cast(AnnotatedData.annotated_string, sa.String) == 
cast(annotation_instance, sa.String)).all()


and inserted the cast'd strings (which shouldn't have been a problem since 
the column types were defined as db.String in the first place).

I wonder if this situation is recognizable and if anyone has any advice on 
dealing with the situation?

Thanks

On Friday, March 4, 2016 at 2:51:15 PM UTC-5, Tim Pierson wrote:
>
> I've seen a couple of issues regarding mssql and drivers handling of 
> strings but I haven't been able to resolve my problem.  
>
> I have a flask app with flask-sqlalchemy models defined and a simple query 
> is throwing the above complaint about decoding the string. 
>
> My connection string:
>
> mssql+pyodbc://[...]/[...]?trusted_connection=yes&charset=utf8&deprecate_large_types=True
>  A 
>
>
> Which is given to the SQLAlchemy class instance db after the initialization 
> of the app with:
>
>
> db.init_app(app)
>
> The offending model:
>
>
> class Annotations(db.Model):
>     id = db.Column(db.Integer, primary_key=True)
>     creation_date = db.Column(db.DateTime, default=datetime.datetime.now)
>     created_by = db.Column(db.String, nullable=False)
>     annotation = db.Column(sqlalchemy.Unicode, nullable=False)
>     annotation_type_id = db.Column(db.Integer, 
> db.ForeignKey('AnnotationTypes.id'))
>
>
>
> The query:
>
>
> db.session.query(Annotations.annotation).all()
>
>
> I've replaced the original column type db.String with sqlalchemy.Unicode on 
> the field that throws the error as per an earlier question on this list but 
> it hasn't resolved the problem.  
>
>
> Could someone offer any pointers?
>
>
> Thanks,
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to