Python3 attempting to query db with string gives "TypeError: string argument without an encoding"

Issue #3692 resolved
Greg R created an issue

My code is pretty straightforward: I'm looking for an entry with an email address.

committed_message = Table.query.filter(
    Table.email_to == email_to
).order_by(Table.created_at.desc()).first()

But I get this in response.

File "/Users/greg/workspace/python/meg-server/venv/lib/python3.5/site-packages/sqlalchemy/orm/query.py", line 2634, in first
ret = list(self[0:1])
File "/Users/greg/workspace/python/meg-server/venv/lib/python3.5/site-packages/sqlalchemy/orm/query.py", line 2457, in __getitem__
return list(res)
File "/Users/greg/workspace/python/meg-server/venv/lib/python3.5/site-packages/sqlalchemy/orm/loading.py", line 86, in instances
util.raise_from_cause(err)
File "/Users/greg/workspace/python/meg-server/venv/lib/python3.5/site-packages/sqlalchemy/util/compat.py", line 200, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "/Users/greg/workspace/python/meg-server/venv/lib/python3.5/site-packages/sqlalchemy/util/compat.py", line 184, in reraise
raise value
File "/Users/greg/workspace/python/meg-server/venv/lib/python3.5/site-packages/sqlalchemy/orm/loading.py", line 71, in instances
rows = [proc(row) for row in fetch]
File "/Users/greg/workspace/python/meg-server/venv/lib/python3.5/site-packages/sqlalchemy/orm/loading.py", line 71, in <listcomp>
rows = [proc(row) for row in fetch]
File "/Users/greg/workspace/python/meg-server/venv/lib/python3.5/site-packages/sqlalchemy/orm/loading.py", line 457, in _instance
unloaded, populators)
File "/Users/greg/workspace/python/meg-server/venv/lib/python3.5/site-   packages/sqlalchemy/orm/loading.py", line 520, in _populate_partial
dict_[key] = getter(row)
File "/Users/greg/workspace/python/meg-server/venv/lib/python3.5/site-packages/sqlalchemy/sql/sqltypes.py", line 859, in process
value = bytes(value)
TypeError: string argument without an encoding

It seems like the problem here is a generic one; when I try to feed bytes() a string argument in Python3 it wants an encoding like utf8. The code in sqltypes falls into this trap

    def result_processor(self, dialect, coltype):
        def process(value):
            if value is not None:
                value = bytes(value)
            return value
        return process

So this would fail:

bytes("foo")

While this is ok

bytes("foo", "utf8")

I'm honestly not really sure why this is happening though because my table schema looks like this.

class Table(db.Model):
    """
    Stores messages for eventual transmission to client or app
    """
    id = db.Column(db.Integer, primary_key=True)
    action = db.Column(db.VARCHAR(8), nullable=False)
    email_to = db.Column(db.Text, nullable=False)
    email_from = db.Column(db.Text, nullable=False)
    message = db.Column(db.Binary, nullable=False)
    created_at = db.Column(db.DateTime, nullable=False)

    def __init__(self, email_to, email_from, message, action):
        self.action = action
        self.email_to = email_to
        self.email_from = email_from
        self.message = message
        self.created_at = datetime.datetime.now()

technically this should only be occurring if email_to was a Binary column

Comments (4)

  1. Log in to comment