【问题标题】:MySQL Error - Field 'id' doesn't have a default valueMySQL 错误 - 字段 'id' 没有默认值
【发布时间】:2016-09-28 01:21:32
【问题描述】:

我是这个 SQL 的新手,我想我还没有完全理解它。我正在尝试安装一个脚本,该脚本执行以下操作以在数据库中插入创建表

我得到的错误是:

错误!
Mysql entry failed Field 'fb_app_id' 没有默认值。

所以我在文件中搜索并找到了创建表格的这段代码。

CREATE TABLE IF NOT EXISTS `global_config` (
  `site_name` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
  `site_url` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
  `meta_description` text COLLATE utf8mb4_unicode_ci NOT NULL,
  `meta_keywords` text COLLATE utf8mb4_unicode_ci NOT NULL,
  `site_theme` varchar(64) COLLATE utf8mb4_unicode_ci NOT NULL,
  `fb_app_id` varchar(64) COLLATE utf8mb4_unicode_ci NOT NULL,
  `fb_app_secret` varchar(64) COLLATE utf8mb4_unicode_ci NOT NULL,
  `fb_app_token` varchar(64) COLLATE utf8mb4_unicode_ci NOT NULL,
  `fb_scope` varchar(500) COLLATE utf8mb4_unicode_ci NOT NULL,
  `tw_app_id` varchar(64) COLLATE utf8mb4_unicode_ci NOT NULL,
  `tw_app_secret` varchar(64) COLLATE utf8mb4_unicode_ci NOT NULL,
  `yt_client_id` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
  `yt_client_secret` varchar(64) COLLATE utf8mb4_unicode_ci NOT NULL,
  `yt_dev_token` varchar(64) COLLATE utf8mb4_unicode_ci NOT NULL,
 `fb_enabled` tinyint(1) NOT NULL,
  `tw_enabled` tinyint(1) NOT NULL,
  `yt_enabled` tinyint(1) NOT NULL,
  `ffmpeg` varchar(64) COLLATE utf8mb4_unicode_ci NOT NULL,
  `seo_url` tinyint(1) NOT NULL,
  `media_plugin_enabled` tinyint(1) NOT NULL,
  `downloader_plugin_enabled` tinyint(1) NOT NULL,
  `image_watermarking_enabled` tinyint(1) NOT NULL,
  `video_watermarking_enabled` tinyint(1) NOT NULL,
  `image_editor_enabled` tinyint(1) NOT NULL,
  `video_editor_enabled` tinyint(1) NOT NULL,
  `disable_all_crons` tinyint(1) NOT NULL,
  `disable_poster_cron` tinyint(1) NOT NULL,
  `disable_hide_delete_cron` tinyint(1) NOT NULL,
  `disable_insights_cron` tinyint(1) NOT NULL,
  `disable_videoeditor_bumping_cron` tinyint(1) NOT NULL,
  `enable_signup` tinyint(1) NOT NULL,
  `enable_maintenance_mode` tinyint(1) NOT NULL,
  `maintenance_message` text COLLATE utf8mb4_unicode_ci NOT NULL,
  `paypal_email` varchar(500) COLLATE utf8mb4_unicode_ci NOT NULL,
  `admin_email` varchar(500) COLLATE utf8mb4_unicode_ci NOT NULL,
  UNIQUE KEY `site_name` (`site_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

究竟是什么导致了这个问题,我该如何解决?

感谢所有帮助。

编辑:Index.php

$(document).on('click', '.app_setup', function(){

var elem = $(this);
var fb_app_id = $('#fb_app_id').val();
var fb_app_secret = $('#fb_app_secret').val();
var tw_app_id = $('#tw_app_id').val();
var tw_app_secret = $('#tw_app_secret').val();
var yt_client_id = $('#yt_client_id').val();
var yt_client_secret = $('#yt_client_secret').val();
var yt_dev_key = $('#yt_dev_key').val();

elem.hide();
notify('wait', 'Setting up...');
$.post('ajax.php', {
    'fb_app_id': fb_app_id,
    'fb_app_secret': fb_app_secret,
    'tw_app_id': tw_app_id,
    'tw_app_secret': tw_app_secret,
    'yt_client_id': yt_client_id,
    'yt_client_secret' : yt_client_secret,
    'yt_dev_key': yt_dev_key,
    'app_setup': 1
}, function(response){
    var data = $.parseJSON(response);
    if(data.error != ''){
        elem.show();
        return notify('error', data.error);
    }
    else{ 
        notify('success', 'App setup successful');
        $('.step3').slideUp();
        $('.step4').slideDown();
    }
});
});

【问题讨论】:

  • 您确定是在创建时收到此错误,而不是在尝试使用表格时收到此错误吗?
  • 我认为创建表不会产生该错误,因此您的问题中没有包含代码。
  • 感谢您的评论,我编辑了问题并添加了我找到的关于 fb_app_id 的代码。希望这可以帮助。它发生在这个网站上:pastebin.com/wdGjpP2u(按下下一步时)

标签: mysql phpmyadmin create-table


【解决方案1】:

我不认为是 CREATE TABLE 语句导致了错误。

错误(很可能)来自执行不为列提供值的 INSERT 语句。当没有为列提供值时,MySQL 将使用该列的默认值。

一种选择是修改表定义:

`fb_app_id` varchar(64) NOT NULL DEFAULT 'foo',

但是考虑到表中定义为 NOT NULL 的其他列的数量,这可能只是将错误移动到下一列。在不检查 INSERT 语句的情况下,我们只是猜测什么“修复”是最合适的。


作为演示:

CREATE TABLE my_t 
( id  INT UNSIGNED NOT NULL PRIMARY KEY
, foo VARCHAR(4)   NOT NULL
);

INSERT INTO my_t (id) VALUES (1); 
-- Error Code: 1364
-- Field 'foo' doesn't have a default value

INSERT INTO my_t (id,foo) VALUES (2,NULL); 
-- Error Code: 1048
-- Column 'foo' cannot be null

ALTER TABLE my_t CHANGE foo foo VARCHAR(4) NOT NULL DEFAULT NULL ;
-- Error Code: 1067
-- Invalid default value for 'foo'

ALTER TABLE my_t CHANGE foo foo VARCHAR(4) NOT NULL DEFAULT '';

INSERT INTO my_t (id) VALUES (3); 
-- 1 row(s) affected

【讨论】:

  • 感谢您的评论,我编辑了问题并添加了我找到的关于 fb_app_id 的代码。希望这会有所帮助。
  • 按NEXT时在此页面上发生:pastebin.com/wdGjpP2u
猜你喜欢
  • 1970-01-01
  • 2013-03-04
  • 2013-09-16
  • 2014-11-09
  • 2020-01-02
  • 1970-01-01
  • 2015-07-19
  • 1970-01-01
相关资源
最近更新 更多