我有一个类似于Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
utility.execute()
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/core/management/__init__.py", line 356, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 204, in handle
fake_initial=fake_initial,
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/db/migrations/executor.py", line 115, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/db/migrations/executor.py", line 145, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/db/migrations/migration.py", line 129, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/db/migrations/operations/fields.py", line 156, in database_forwards
schema_editor.remove_field(from_model, from_model._meta.get_field(self.name))
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/db/backends/sqlite3/schema.py", line 256, in remove_field
self._remake_table(model, delete_field=field)
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/db/backends/sqlite3/schema.py", line 206, in _remake_table
self.quote_name(model._meta.db_table),
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 120, in execute
cursor.execute(sql, params)
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/Users/zorgan/Desktop/app/lib/python3.5/site-packages/django/db/backends/sqlite3/base.py", line 328, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: datatype mismatch
的架构,并且使用sequalize,我想使用product: { ... ,ratings: [ {rating: 3} ] }
添加属性product.avg_rating
。
这是我的代码:
sequelize
但我一直收到错误:
在没有GROUP BY的聚合查询中,SELECT列表的表达式#1 包含nonaggregated column&#39; text_rewriter.languageCombination.id&#39 ;; 这与sql_mode = only_full_group_by
不兼容
目标输出:
sequelize.models.product.findAll({
include: [{
model: sequelize.models.rating,
as: 'ratings',
attributes: {
include: ['rating',[sequelize.fn('AVG', 'ratings.rating'), 'avg_rating']]
},
group: ['rating.rating'],//also tried ratings.rating, and just rating
}],
}).then(products=>{...})
我缺少什么想法?我见过很多例子,但没有一个像我发现的那样使用包括。
答案 0 :(得分:0)
sequelize.models.product.findAll({
include: [{
model: sequelize.models.rating,
as: 'ratings',
attributes: ['avg_rating'] //this is column name here
}],
}).then(products=>{...})
这将返回: -
products: [
{product: 'product name',
ratings: [...],
rating : { //here all the attributes you wanted, in this case only 'avg_rating' }
},
{...}
]
但是在使用它之前,你必须在产品表和评级表之间定义关系。
表:产品有id,名称
表:评级有id,rating,product_id
在上述情况下,关系将是'rating.belongsTo(product)OR product.hasMay(rating)'
答案 1 :(得分:-1)
MySQL实现对功能依赖性的检测。如果启用了ONLY_FULL_GROUP_BY SQL
模式(默认情况下是默认模式),MySQL将拒绝选择列表,HAVING
条件或ORDER BY
列表所引用的非聚合列的查询在GROUP BY
子句中命名的功能在功能上也不依赖于它们。
该错误与sql_mode
有关,您需要在数据库控制台上执行以下命令。
SET GLOBAL sql_mode = '';