简体   繁体   English

MySQL错误-字段“ id”没有默认值

[英]MySQL Error - Field 'id' doesn't have a default value

I am new to this SQL and I think I do not understand it fully yet. 我刚接触此SQL,我想我还不太了解它。 I am trying to install a script which does the following to insert create tables in a database 我正在尝试安装一个脚本,该脚本执行以下操作以在数据库中插入创建表

The error I get is: 我得到的错误是:

Error! 错误!
Mysql entry failed Field 'fb_app_id' doesn't have a default value. MySQL输入失败字段'fb_app_id'没有默认值。

So I searched in the files and found this piece of code which is creating the table. 因此,我搜索了文件并找到了创建表的这段代码。

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;

What exactly is causing the issue now and how can I solve it? 现在到底是什么原因导致该问题,我该如何解决?

All help is appreciated. 感谢所有帮助。

EDIT: Index.php 编辑: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();
    }
});
});

I don't think it's the CREATE TABLE statement that is causing the error. 我不认为是导致错误的CREATE TABLE语句。

The error is (most likely) from executing an INSERT statement that doesn't supply a value for the column. 该错误(最有可能)是由于执行了不为该列提供值的INSERT语句。 When a value isn't supplied for a column, MySQL will use the default value for the column. 如果没有为列提供值,则MySQL将使用该列的默认值。

One option would be to modify the table definition: 一种选择是修改表定义:

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

But given the number of other columns in the table that are defined as NOT NULL, that might just move the error to the next column. 但是给定表中定义为NOT NULL的其他列的数量,这可能会将错误移至下一列。 Without examining the INSERT statement, we're just guessing what "fix" is most appropriate. 在不检查INSERT语句的情况下,我们只是在猜测哪种“修复”最合适。


As a demonstration: 作为演示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM