2017-01-03 11 views
0

問題は、日付の列が2つあり、sqlalchemyがUnicodeに間違って変換しているようです。これはTypeErrorをスローします。プロセッサ関数のdatetime.datetimeではなくstringまたはbufferでなければなりません。SQLAlchemy - TypeError:datetime.datetimeでない文字列またはバッファでなければなりません

私が(デバッガからいくつかの注釈付き)スタックトレースを持た:

from app import db 

class Base_Track_User(db.Model): 
    """ 
    A base class with additional fields that track which user added and updates each instance 
    """ 
    __table_args__ = {'schema':DEFAULT_SCHEMA} 
    __abstract__ = True 

    id = db.Column(db.Integer, primary_key=True, 
     info={'widget': HiddenInput()} 
    ) 
    date_created = db.Column(db.DateTime, default=db.func.current_timestamp()) 
    date_modified = db.Column(db.DateTime, default=db.func.current_timestamp(), 
              onupdate=db.func.current_timestamp()) 
    created_by = db.Column(db.String(255), default=get_user_id) 
    modified_by = db.Column(db.String(255), default=get_user_id, onupdate=get_user_id) 

class Tap_Record(Base_Track_User): 
    """ 
    the tap card model with all the information regarding 
    each properties water and sewer lines 
    """ 
    __tablename__ = 'util_tap' 
    __table_args__ = { 
     'schema': 'dbo', 
    } 

    id = db.Column(db.Integer, primary_key=True) 
    parcelid = db.Column(db.String(255)) 
    status = db.Column(db.String(255)) 
    street_add = db.Column(db.String(255)) 
    prop_desc = db.Column(db.String(255)) 
    service_num = db.Column(db.Integer) 
    meter_desc = db.Column(db.String(255)) 
    corp_desc = db.Column(db.String(255)) 
    curb_stop = db.Column(db.String(255)) 
    water_material = db.Column(db.String(255)) 
    water_date = db.Column(db.String(255)) 
    laid_by = db.Column(db.String(255)) 
    re_laid_by = db.Column(db.String(255)) 
    san_service = db.Column(db.String(255)) 
    san_laid = db.Column(db.String(255)) 
    san_material = db.Column(db.String(255)) 
    san_date = db.Column(db.DateTime) 
    remarks = db.Column(db.Text()) 
    file_url = db.Column(db.Text()) 

ここに私のDBスキーマがあります:

[2017-01-03 11:20:20,026] {C:\\www\\flask_app\\virtualenv\\lib\\site-packages\flask\app.py:1587} ERROR - Exception on /api/util_tap [GET] 
Traceback (most recent call last): 
    File "C:\\www\\flask_app\\virtualenv\\lib\\site-packages\flask\app.py", line 1988, in wsgi_app 
    response = self.full_dispatch_request() 
    File "C:\\www\\flask_app\\virtualenv\\lib\\site-packages\flask\app.py", line 1641, in full_dispatch_request 
    rv = self.handle_user_exception(e) 
    File "C:\\www\\flask_app\\virtualenv\\lib\\site-packages\flask_cors\extension.py", line 161, in wrapped_function 
    return cors_after_request(app.make_response(f(*args, **kwargs))) 
    File "C:\\www\\flask_app\\virtualenv\\lib\\site-packages\flask\app.py", line 1544, in handle_user_exception 
    reraise(exc_type, exc_value, tb) 
    File "C:\\www\\flask_app\\virtualenv\\lib\\site-packages\flask\app.py", line 1639, in full_dispatch_request 
    rv = self.dispatch_request() 
    File "C:\\www\\flask_app\\virtualenv\\lib\\site-packages\flask\app.py", line 1625, in dispatch_request 
    return self.view_functions[rule.endpoint](**req.view_args) 
    File "c:\\www\\flask_app\\virtualenv\\src\\flask-restless\flask_restless\views\base.py", line 391, in new_func 
    return func(*args, **kw) 
    File "c:\\www\\flask_app\\virtualenv\\src\\flask-restless\flask_restless\views\base.py", line 340, in new_func 
    return func(*args, **kw) 
    File "c:\\www\\flask_app\\virtualenv\\src\\flask-restless\flask_restless\views\base.py", line 244, in new_func 
    return func(*args, **kw) 
    File "C:\\www\\flask_app\\virtualenv\\lib\\site-packages\flask\views.py", line 84, in view 
    return self.dispatch_request(*args, **kwargs) 
    File "C:\\www\\flask_app\\virtualenv\\lib\\site-packages\flask\views.py", line 149, in dispatch_request 
    return meth(*args, **kwargs) 
    File "c:\\www\\flask_app\\virtualenv\\src\\flask-restless\flask_restless\views\base.py", line 444, in wrapped 
    return func(*args, **kw) 
    File "c:\\www\\flask_app\\virtualenv\\src\\flask-restless\flask_restless\views\resources.py", line 398, in get 
    return self._get_collection() 
    File "c:\\www\\flask_app\\virtualenv\\src\\flask-restless\flask_restless\views\resources.py", line 369, in _get_collection 
    group_by=group_by, single=single) 
    File "c:\\www\\flask_app\\virtualenv\\src\\flask-restless\flask_restless\views\base.py", line 1717, in _get_collection_helper 
    included = self.get_all_inclusions(instances) 
    File "c:\\www\\flask_app\\virtualenv\\src\\flask-restless\flask_restless\views\base.py", line 1402, in get_all_inclusions 
    to_include = set(chain(map(self.resources_to_include, instances))) 

### instances is a BaseQuery object 
### str(instances) is: 
'SELECT dbo.util_tap.date_created AS dbo_util_tap_date_created, dbo.util_tap.date_modified AS dbo_util_tap_date_modified, dbo.util_tap.created_by AS dbo_util_tap_created_by, dbo.util_tap.modified_by AS dbo_util_tap_modified_by, dbo.util_tap.id AS dbo_util_tap_id, dbo.util_tap.parcelid AS dbo_util_tap_parcelid, dbo.util_tap.status AS dbo_util_tap_status, dbo.util_tap.street_add AS dbo_util_tap_street_add, dbo.util_tap.prop_desc AS dbo_util_tap_prop_desc, dbo.util_tap.service_num AS dbo_util_tap_service_num, dbo.util_tap.meter_desc AS dbo_util_tap_meter_desc, dbo.util_tap.corp_desc AS dbo_util_tap_corp_desc, dbo.util_tap.curb_stop AS dbo_util_tap_curb_stop, dbo.util_tap.water_material AS dbo_util_tap_water_material, dbo.util_tap.water_date AS dbo_util_tap_water_date, dbo.util_tap.laid_by AS dbo_util_tap_laid_by, dbo.util_tap.re_laid_by AS dbo_util_tap_re_laid_by, dbo.util_tap.san_service AS dbo_util_tap_san_service, dbo.util_tap.san_laid AS dbo_util_tap_san_laid, dbo.util_tap.san_material AS dbo_util_tap_san_material, dbo.util_tap.san_date AS dbo_util_tap_san_date, dbo.util_tap.remarks AS dbo_util_tap_remarks, dbo.util_tap.file_url AS dbo_util_tap_file_url \nFROM dbo.util_tap ORDER BY dbo.util_tap.id ASC 

    File "C:\\www\\flask_app\\virtualenv\\lib\\site-packages\sqlalchemy\orm\loading.py", line 90, in instances 
    util.raise_from_cause(err) 
    File "C:\\www\\flask_app\\virtualenv\\lib\\site-packages\sqlalchemy\util\compat.py", line 202, in raise_from_cause 
    reraise(type(exception), exception, tb=exc_tb, cause=cause) 
    File "C:\\www\\flask_app\\virtualenv\\lib\\site-packages\sqlalchemy\orm\loading.py", line 75, in instances 
    rows = [proc(row) for row in fetch] 
    File "C:\\www\\flask_app\\virtualenv\\lib\\site-packages\sqlalchemy\orm\loading.py", line 437, in _instance 
    loaded_instance, populate_existing, populators) 
    File "C:\\www\\flask_app\\virtualenv\\lib\\site-packages\sqlalchemy\orm\loading.py", line 498, in _populate_full 
    dict_[key] = getter(row) 
    File "C:\\www\\flask_app\\virtualenv\\lib\\site-packages\sqlalchemy\engine\result.py", line 93, in __getitem__ 
    return processor(self._row[index]) 

### self is <broken repr (TypeError: must be string or buffer, not datetime.datetime)> 

### self.row is (datetime.datetime(2014, 11, 17, 0, 0), datetime.datetime(2014, 11, 17, 0, 0), u'pschulz', u'pschulz', 1259, u'1836228001', None, u'2100 GRANT ST - GOODWILL', u'BLOCK 1 LOT 2 REINEKES-1ST ADD', 7289, None, None, u'IN PARKING LOT 49\' S. OF BLDG & 69\'3" SW OF SE CORNER OF BLDG TO 6" VALVE, 1" VALVE 2\' N. & 2\' W. OF 6" VALVE', u'ST SECT-6" DUCTILE     YD SEC-1" BLUE POLY W/TRACER', datetime.datetime(2014, 11, 7, 0, 0), u'MN UTILITIES & EXCAVATING -10/7/2014', None, u"NOT SAME TRENCH AS WATER, SEWER DEPTH-8', WATERMAIN DEPTH 7'", u'MN UTILITIES & EXCAVATING -10/7/2014', u'ST SEC-               YD SEC-4" 3034', datetime.datetime(2014, 11, 7, 0, 0), u'BLDG HAD EXISTING SEWER INTO PROPERTY AND WAS EXTENDED TO NEW BLDG PAD FROM PREVIOUS BLDG LOCATION.\r\nSEE BACK OF CARD FOR DRAWING', u'/gis/utilities/static/uploads/tap/1259.pdf') 

    File "C:\\www\\flask_app\\virtualenv\\lib\\site-packages\sqlalchemy\processors.py", line 85, in process 
    return decoder(value, errors)[0] 
    File "C:\Python27\ArcGISx6410.4\lib\encodings\utf_8.py", line 16, in decode 
    return codecs.utf_8_decode(input, errors, True) 
TypeError: must be string or buffer, not datetime.datetime 

そして、ここではreleventのORMモデル(S)であります

db schema

+0

適切なSqlalchemyモデルに従って、クラス内で 'def __repr __(self):...'を宣言してください – mootmoot

+0

@mootmoot関数 '__repr __(self)'を追加できました。 – roemhildtg

+1

各列に対して適切な '__repr__'宣言を使用すると、無作為例外broken self reprではなく、問題のある列を特定することができます。 – mootmoot

答えて

0

これは明らかなことです...もう一度。私はPythonモデルにString型の列を設定しましたが、SQL Serverではdatetime型に設定されていましたが、これは問題を引き起こしていました。

関連する問題