ubuntu 错误代码:BLOB、TEXT、GEOMETRY或JSON数据行'body'不能有预设值

taor4pac  于 2022-11-02  发布在  其他
关注(0)|答案(6)|浏览(177)

我有迁移:

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:迁移显示错误:

Myql 2::错误:BLOB、TEXT、GEOMETRY或JSON数据行'body'不能有预设值:更改表格blog_posts更改bodybody文本默认值“”非空/主页/用户/项目/项目名称/数据库/迁移/20120508203410更改博客帖子的默认值正文. rb:3:in 'up'

我使用ubuntu16.04,
mysql服务器5.7(版本:5.7.12-0ubuntu1.1版本号:5.7.12-版本号:第5.7.11节
我在谷歌上找过了,解决这个问题需要改变
SQL-模式=“严格事务处理表,无自动创建用户,无引擎替换”

sql-mode="NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"

但是我的.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/

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

vcudknz3

vcudknz31#

您需要找到正确的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”

vybvopom

vybvopom2#

我找到了解决办法:需要在我的.cnf中添加

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

lmvvr0a83#

我在本地机器上使用mysql从xampp进行迁移时没有遇到任何问题。只是在生产服务器上开始出现故障。我发现xampp运行的是mariadb而不是普通的mysql。所以在服务器上使用mariaDB解决了我的问题。

vkc1a9a2

vkc1a9a24#

再检查一下sql代码,上面的错误很清楚的说'body'不是默认值

范例:

`completed` text NOT NULL  DEFAULT 'false',

删除后默认值

`completed` text NOT NULL,

现在希望你的错误已经消失了。对我的英语感到抱歉。

zpqajqem

zpqajqem5#

我是这样解决这个问题的:我曾经想在一个服务器上导出我的mySql数据库,然后导入到另一个服务器(CPanel主机)。每次我尝试时,phpMyAdmin总是在数据库表的一个字段上给出这个错误。然后我转到我的源主机;打开phpMyAdmin并打开那个表结构,删除那个字段的默认值。然后我又导出了一次,这次成功了。

quhf5bfb

quhf5bfb6#

从MySQL 8.0.13开始,现在可以为TEXT/BLOB字段指定默认值。但是,文字默认值需要指定为表达式。
例如,这是有效的:

`title` text COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT ('Title')

但这会产生一个错误:

`title` text COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'Title'

相关问题