Mysql2::错误:BLOB、TEXT、GEOMETRY 或 JSON 列 'body' 不能有默认值

标签 mysql ruby-on-rails ubuntu

我有迁移:

lass ChangeDefaultValueBodyForBlogPosts < ActiveRecord::Migration
  def up
    change_column :blog_posts, :body, :text, :null => false
    change_column :comments, :text, :text, :null => false
  end

  def down
    change_column :comments, :text, :text, :default => nil
    change_column :blog_posts, :body, :text, :default => nil
  end
end

rake db:migrate 显示错误:

Mysql2::错误:BLOB、TEXT、GEOMETRY 或 JSON 列“body”不能有默认值:ALTER TABLE blog_posts CHANGE body 正文 text DEFAULT '' NOT NULL/home/user/projects/projectname/db/migrate/20120508203410_change_default_value_body_for_blog_posts.rb:3:in `up'

我用的是ubuntu 16.04,

mysql server 5.7(版本: 5.7.12-0ubuntu1.1 版本:5.7.12-0ubuntu1 版本:5.7.11-0ubuntu6 )

我查了一下google,解决这个问题需要修改

sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"

sql-mode="NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"

但是 my.conf 文件是空的。

#
# The MySQL database server configuration file.
#
# You can copy this to one of:
# - "/etc/mysql/my.cnf" to set global options,
# - "~/.my.cnf" to set user-specific options.
# 
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

#
# * IMPORTANT: Additional settings that can override those from this file!
#   The files must end with '.cnf', otherwise they'll be ignored.
#

!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/

这个问题怎么解决,求助。谢谢。

最佳答案

您需要找到正确的 my.cnf:

sudo find / -name "*.cnf"

/etc/mysql/mysql.conf.d/mysqld.cnf
/etc/mysql/my.cnf
/etc/mysql/mysql.cnf
/etc/mysql/conf.d/mysqldump.cnf
/etc/mysql/conf.d/mysql.cnf

我根据编辑/etc/mysql/mysql.conf.d/mysqld.cnf

strace mysql ";" 2>&1  | grep cnf

stat("/etc/my.cnf", 0x7ffda9472660)     = -1 ENOENT (No such file or directory)
stat("/etc/mysql/my.cnf", {st_mode=S_IFREG|0644, st_size=683, ...}) = 0
open("/etc/mysql/my.cnf", O_RDONLY)     = 3
stat("/etc/mysql/conf.d/mysql.cnf", {st_mode=S_IFREG|0644, st_size=8, ...}) = 0
open("/etc/mysql/conf.d/mysql.cnf", O_RDONLY) = 4
stat("/etc/mysql/conf.d/mysqldump.cnf", {st_mode=S_IFREG|0644, st_size=55, ...}) = 0
open("/etc/mysql/conf.d/mysqldump.cnf", O_RDONLY) = 4
stat("/etc/mysql/mysql.conf.d/mysqld.cnf", {st_mode=S_IFREG|0644, st_size=3034, ...}) = 0
open("/etc/mysql/mysql.conf.d/mysqld.cnf", O_RDONLY) = 4
stat("/etc/mysql/mysql.conf.d/mysqld_safe_syslog.cnf", {st_mode=S_IFREG|0644, st_size=21, ...}) = 0
open("/etc/mysql/mysql.conf.d/mysqld_safe_syslog.cnf", O_RDONLY) = 4
stat("/root/.my.cnf", 0x7ffda9472660)   = -1 ENOENT (No such file or directory)
stat("/root/.mylogin.cnf", 0x7ffda9472660) = -1 ENOENT (No such file or directory)

找到正确的文件后,在 [mysqld] 语句下面添加 sql-mode="NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"

关于Mysql2::错误:BLOB、TEXT、GEOMETRY 或 JSON 列 'body' 不能有默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38433603/

相关文章:

angularjs - 验证部署 : FAILED using MUP for my meteor app

php - MySQL 资源无效

ruby-on-rails - 检测这是 iframe 加载还是直接加载

ruby-on-rails - 覆盖 Rails 强参数中的一个属性

jquery - Bootstrap 图片库,作者:blueimp : How to disable the wheel handler and keyboard handler

ubuntu - 使用 Sublime Text 编辑我用 vagrant 创建的本地服务器上的文件的解决方案?

Ubuntu 中的 OpenGL/SDL 问题

mysql - 是否可以使用身份生成进行 Hibernate 批处理?

mysql - 高插入负载的最佳 mysql 表格式是什么?

mysql - SQL 外键约束错误 1215