我在pymysql和mysql中使用flask sqlachemy
我正在尝试使用flask_fixture将数据插入数据库时测试我的应用程序。
我想这去了数据库,但它返回一个错误
'Department' object has no attribute 'translate'
所以我不明白为什么会抛出该错误
from flask_login import (UserMixin, current_user, LoginManager)
from flask_dance.consumer.storage.sqla import OAuthConsumerMixin
from werkzeug.security import generate_password_hash, check_password_hash
from . import db
class Department(db.Model):
# this model handles everything to do with the department information
__tablename__ = 'departments'
id = db.Column(db.Integer, primary_key = True)
name = db.Column(db.String(64), nullable = False)
description = db.Column(db.String(300), nullable = False) ```
Here is the file I want to insert using `flask_fixtures`
[
{
"table": "departments",
"records": [{
"id": 8,"name": "Kinds","description": "all goods about kids"},
{"id": 9,"name": "panties", "description": "this is a women section"}
]
}
]
这里是抛出错误
Test initial data for Client model. ... [INFO] setting up fixtures... in File "/home/rashid/Desktop/Ecomerce_api/venv/lib/python3.6/site-packages/flask_fixtures/__init__.py", line 84, in setup
ERROR
======================================================================
ERROR: test_client_data (test_category_department.TestCatDepartRoutes)
Test initial data for Client model.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/rashid/Desktop/Ecomerce_api/venv/lib/python3.6/site-packages/flask_fixtures/__init__.py", line 187, in handler
setup_fn(obj)
File "/home/rashid/Desktop/Ecomerce_api/venv/lib/python3.6/site-packages/flask_fixtures/__init__.py", line 247, in child_fn
call_method(obj, fn)
File "/home/rashid/Desktop/Ecomerce_api/venv/lib/python3.6/site-packages/flask_fixtures/__init__.py", line 227, in call_method
method.__get__(instance, owner)()
File "/home/rashid/Desktop/Ecomerce_api/tests/test_base.py", line 45, in setUp
data=json.dumps(self.reviews))
File "/home/rashid/Desktop/Ecomerce_api/venv/lib/python3.6/site-packages/werkzeug/test.py", line 840, in post
return self.open(*args, **kw)
File "/home/rashid/Desktop/Ecomerce_api/venv/lib/python3.6/site-packages/flask/testing.py", line 127, in open
follow_redirects=follow_redirects)
File "/home/rashid/Desktop/Ecomerce_api/venv/lib/python3.6/site-packages/werkzeug/test.py", line 803, in open
response = self.run_wsgi_app(environ, buffered=buffered)
File "/home/rashid/Desktop/Ecomerce_api/venv/lib/python3.6/site-packages/werkzeug/test.py", line 716, in run_wsgi_app
rv = run_wsgi_app(self.application, environ, buffered=buffered)
File "/home/rashid/Desktop/Ecomerce_api/venv/lib/python3.6/site-packages/werkzeug/test.py", line 923, in run_wsgi_app
app_rv = app(environ, start_response)
File "/home/rashid/Desktop/Ecomerce_api/venv/lib/python3.6/site-packages/flask/app.py", line 1997, in __call__
return self.wsgi_app(environ, start_response)
File "/home/rashid/Desktop/Ecomerce_api/venv/lib/python3.6/site-packages/flask/app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "/home/rashid/Desktop/Ecomerce_api/venv/lib/python3.6/site-packages/flask/app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/rashid/Desktop/Ecomerce_api/venv/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/home/rashid/Desktop/Ecomerce_api/venv/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/home/rashid/Desktop/Ecomerce_api/venv/lib/python3.6/site-packages/flask/app.py", line 1614, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/rashid/Desktop/Ecomerce_api/venv/lib/python3.6/site-packages/flask/app.py", line 1517, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/rashid/Desktop/Ecomerce_api/venv/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/home/rashid/Desktop/Ecomerce_api/venv/lib/python3.6/site-packages/flask/app.py", line 1612, in full_dispatch_request
rv = self.dispatch_request()
File "/home/rashid/Desktop/Ecomerce_api/venv/lib/python3.6/site-packages/flask/app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/rashid/Desktop/Ecomerce_api/api/Customer/user.py", line 20, in decorated
token=request.headers['x-access-token']).count() == 0:
File "/home/rashid/Desktop/Ecomerce_api/venv/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 3124, in count
return self.from_self(col).scalar()
File "/home/rashid/Desktop/Ecomerce_api/venv/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 2877, in scalar
ret = self.one()
File "/home/rashid/Desktop/Ecomerce_api/venv/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 2848, in one
ret = self.one_or_none()
File "/home/rashid/Desktop/Ecomerce_api/venv/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 2818, in one_or_none
ret = list(self)
File "/home/rashid/Desktop/Ecomerce_api/venv/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 2888, in __iter__
self.session._autoflush()
File "/home/rashid/Desktop/Ecomerce_api/venv/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 1434, in _autoflush
self.flush()
File "/home/rashid/Desktop/Ecomerce_api/venv/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 2254, in flush
self._flush(objects)
File "/home/rashid/Desktop/Ecomerce_api/venv/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 2380, in _flush
transaction.rollback(_capture_exception=True)
File "/home/rashid/Desktop/Ecomerce_api/venv/lib/python3.6/site-packages/sqlalchemy/util/langhelpers.py", line 66, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "/home/rashid/Desktop/Ecomerce_api/venv/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 187, in reraise
raise value
File "/home/rashid/Desktop/Ecomerce_api/venv/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 2344, in _flush
flush_context.execute()
File "/home/rashid/Desktop/Ecomerce_api/venv/lib/python3.6/site-packages/sqlalchemy/orm/unitofwork.py", line 391, in execute
rec.execute(self)
File "/home/rashid/Desktop/Ecomerce_api/venv/lib/python3.6/site-packages/sqlalchemy/orm/unitofwork.py", line 556, in execute
uow
File "/home/rashid/Desktop/Ecomerce_api/venv/lib/python3.6/site-packages/sqlalchemy/orm/persistence.py", line 181, in save_obj
mapper, table, insert)
File "/home/rashid/Desktop/Ecomerce_api/venv/lib/python3.6/site-packages/sqlalchemy/orm/persistence.py", line 866, in _emit_insert_statements
execute(statement, params)
File "/home/rashid/Desktop/Ecomerce_api/venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 948, in execute
return meth(self, multiparams, params)
File "/home/rashid/Desktop/Ecomerce_api/venv/lib/python3.6/site-packages/sqlalchemy/sql/elements.py", line 269, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "/home/rashid/Desktop/Ecomerce_api/venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1060, in _execute_clauseelement
compiled_sql, distilled_params
File "/home/rashid/Desktop/Ecomerce_api/venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1200, in _execute_context
context)
File "/home/rashid/Desktop/Ecomerce_api/venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1416, in _handle_dbapi_exception
util.reraise(*exc_info)
File "/home/rashid/Desktop/Ecomerce_api/venv/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 187, in reraise
raise value
File "/home/rashid/Desktop/Ecomerce_api/venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1193, in _execute_context
context)
File "/home/rashid/Desktop/Ecomerce_api/venv/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 507, in do_execute
cursor.execute(statement, parameters)
File "/home/rashid/Desktop/Ecomerce_api/venv/lib/python3.6/site-packages/pymysql/cursors.py", line 168, in execute
query = self.mogrify(query, args)
File "/home/rashid/Desktop/Ecomerce_api/venv/lib/python3.6/site-packages/pymysql/cursors.py", line 147, in mogrify
query = query % self._escape_args(args, conn)
File "/home/rashid/Desktop/Ecomerce_api/venv/lib/python3.6/site-packages/pymysql/cursors.py", line 127, in _escape_args
return {key: conn.literal(val) for (key, val) in args.items()}
File "/home/rashid/Desktop/Ecomerce_api/venv/lib/python3.6/site-packages/pymysql/cursors.py", line 127, in <dictcomp>
return {key: conn.literal(val) for (key, val) in args.items()}
File "/home/rashid/Desktop/Ecomerce_api/venv/lib/python3.6/site-packages/pymysql/connections.py", line 467, in literal
return self.escape(obj, self.encoders)
File "/home/rashid/Desktop/Ecomerce_api/venv/lib/python3.6/site-packages/pymysql/connections.py", line 460, in escape
return converters.escape_item(obj, self.charset, mapping=mapping)
File "/home/rashid/Desktop/Ecomerce_api/venv/lib/python3.6/site-packages/pymysql/converters.py", line 27, in escape_item
val = encoder(val, mapping)
File "/home/rashid/Desktop/Ecomerce_api/venv/lib/python3.6/site-packages/pymysql/converters.py", line 118, in escape_unicode
return u"'%s'" % _escape_unicode(value)
File "/home/rashid/Desktop/Ecomerce_api/venv/lib/python3.6/site-packages/pymysql/converters.py", line 73, in _escape_unicode
return value.translate(_escape_table)
AttributeError: 'Department' object has no attribute 'translate'