【问题标题】:Mysql2::Error: BLOB, TEXT, GEOMETRY or JSON column 'body' can't have a default valueMysql2::Error: BLOB、TEXT、GEOMETRY 或 JSON 列 'body' 不能有默认值
【发布时间】:2016-11-20 21:03:03
【问题描述】:

我有迁移:

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::Error: BLOB, TEXT, GEOMETRY or JSON column 'body' can't have a default value: ALTER TABLE blog_posts CHANGE body 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服务器5.7(版本:5.7.12-0ubuntu1.1 版本:5.7.12-0ubuntu1 版本:5.7.11-0ubuntu6 )

我查了谷歌,解决这个问题需要改变

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/

如何解决这个问题,请帮忙。谢谢你。

【问题讨论】:

    标签: mysql ruby-on-rails ubuntu


    【解决方案1】:

    你需要找到正确的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"

    【讨论】:

      【解决方案2】:

      我找到了解决方案: 需要在my.cnf中添加

      [mysqld]
      sql-mode="NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
      

      【讨论】:

      • 就我而言,我通过在 MySQL 配置文件(在我的情况下为 /etc/mysql/mysql.conf.d/mysqld.cnf)中设置 sql-mode="" 并重新启动 MySQL 来解决它之后的服务器(使用 sudo systemctl restart mysql )。因此,更改 MySQL 配置中的设置可能会有所帮助。
      【解决方案3】:

      使用来自 xampp 的 mysql 在我的本地计算机上迁移没有问题。只有在生产服务器上它开始失败。我发现 xampp 运行 mariadb 而不是普通的 mysql。所以在服务器上使用 mariaDB 解决了我的问题。

      【讨论】:

        【解决方案4】:

        再次检查你的sql代码错误上面明确说'body'不是默认值
        示例:

         `completed` text NOT NULL  DEFAULT 'false',
        

        删除后默认值

        `completed` text NOT NULL,
        

        现在希望您的错误消失了。 对不起我的英语。

        【讨论】:

        • 注意:如果您依赖查询,这可能会破坏您的代码,如果您省略字段completed,它将获得默认值。你也必须相应地调整你的代码
        【解决方案5】:

        我通过这种方式解决了这个问题:我曾经想在一台服务器上导出我的 mySql 数据库,然后将其导入另一台服务器(CPanel 主机)。每次我尝试时,phpMyAdmin 都会在其中一个数据库表的字段之一上不断给出此错误。然后我去了我的源主机;打开 phpMyAdmin 并打开该表结构并删除该字段的默认值。然后我再次导出,这次成功了。

        【讨论】:

          猜你喜欢
          • 2018-11-21
          • 1970-01-01
          • 1970-01-01
          • 2023-03-12
          • 1970-01-01
          • 1970-01-01
          • 2020-06-09
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多