Comments - Field 'xxxx' doesn't have a default value

3 years, 4 months ago Bruce Therrien

fax, company, and org_sponsor are also giving the error.

MyISAM storage

-- phpMyAdmin SQL Dump
-- version 4.6.6deb4+deb9u1
-- https://www.phpmyadmin.net/
--
-- Host: localhost
-- Generation Time: May 10, 2021 at 09:37 AM
-- Server version: 10.3.27-MariaDB-0+deb10u1-log
-- PHP Version: 7.3.27-1~deb10u1

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `opencart`
--

-- --------------------------------------------------------

--
-- Table structure for table `customer`
--

CREATE TABLE `customer` (
  `customer_id` int(11) NOT NULL,
  `customer_group_id` int(11) NOT NULL,
  `store_id` int(11) NOT NULL DEFAULT 0,
  `firstname` varchar(64) NOT NULL,
  `lastname` varchar(64) NOT NULL,
  `email` varchar(96) NOT NULL,
  `telephone` varchar(32) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `fax` varchar(32) NOT NULL,
  `password` varchar(40) NOT NULL,
  `salt` varchar(9) NOT NULL,
  `cart` text DEFAULT NULL,
  `wishlist` text DEFAULT NULL,
  `newsletter` tinyint(1) NOT NULL DEFAULT 0,
  `address_id` int(11) NOT NULL DEFAULT 0,
  `custom_field` text NOT NULL,
  `ip` varchar(40) NOT NULL,
  `status` tinyint(1) NOT NULL,
  `approved` tinyint(1) NOT NULL,
  `safe` tinyint(1) NOT NULL,
  `token` varchar(255) NOT NULL,
  `date_added` timestamp NOT NULL DEFAULT current_timestamp(),
  `wmmw_id` varchar(64) NOT NULL,
  `photo` varchar(50) NOT NULL,
  `voice` varchar(20) NOT NULL,
  `voice2` varchar(20) NOT NULL,
  `manager` int(2) NOT NULL,
  `rank` varchar(20) NOT NULL,
  `rank_icon` varchar(20) NOT NULL,
  `pagetext_en` varchar(300) NOT NULL,
  `pagetext_fr` varchar(300) NOT NULL,
  `pp_email` varchar(96) NOT NULL,
  `validated` varchar(12) NOT NULL,
  `visitor` int(2) NOT NULL,
  `candidate` int(2) NOT NULL,
  `reseller` varchar(10) NOT NULL DEFAULT 'No',
  `coop_page` int(2) NOT NULL,
  `wmmw_domain` varchar(128) DEFAULT NULL,
  `volunteer` int(2) NOT NULL DEFAULT 0,
  `level` varchar(30) DEFAULT NULL,
  `authorized` varchar(20) NOT NULL,
  `target` decimal(12,2) NOT NULL,
  `members` int(12) NOT NULL,
  `charter` varchar(50) NOT NULL,
  `raffle` varchar(25) NOT NULL,
  `raffle2` varchar(25) NOT NULL,
  `raffle3` varchar(25) NOT NULL,
  `sponsor_id` varchar(16) NOT NULL,
  `coordinator_level` varchar(25) NOT NULL,
  `beta` varchar(20) DEFAULT NULL,
  `org_sponsor` varchar(16) NOT NULL,
  `coordinator` varchar(12) NOT NULL DEFAULT 'No',
  `angel` varchar(12) NOT NULL,
  `apprentice` varchar(12) NOT NULL,
  `angel_id` varchar(40) NOT NULL,
  `comptroller` varchar(12) NOT NULL,
  `deposit` varchar(20) NOT NULL,
  `gencomp` varchar(12) NOT NULL,
  `comptroller_id` varchar(20) NOT NULL,
  `gencomp_id` varchar(20) NOT NULL,
  `gencomp_id2` varchar(20) NOT NULL,
  `coordinator_id` int(4) NOT NULL DEFAULT 0,
  `p2` varchar(30) NOT NULL,
  `timezone` varchar(30) NOT NULL,
  `raffle4` varchar(12) NOT NULL,
  `raffle5` varchar(12) NOT NULL,
  `primary_raffle` varchar(12) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

--
-- Indexes for dumped tables
--

--
-- Indexes for table `customer`
--
ALTER TABLE `customer`
  ADD PRIMARY KEY (`customer_id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `customer`
--
ALTER TABLE `customer`
  MODIFY `customer_id` int(11) NOT NULL AUTO_INCREMENT;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
 
3 years, 4 months ago Ian Gilfillan

You haven't provided the queries that are giving the error, but since this looks like an OpenCart installation, it's probably an issue with the OpenCart application sometimes not populating a field. With the definition you've provided, you will get the error you mentioned if you try an insert without specifying a particular value for a NOT NULL field. A query like

ALTER TABLE customer MODIFY org_sponsor VARCHAR(16) NOT NULL DEFAULT '';

or

ALTER TABLE customer MODIFY org_sponsor VARCHAR(16) NULL;

will work around the error you mention, but you should test if this doesn't have other unforeseen consequences. The application may not handle NULL values correctly, for example.

 
3 years, 4 months ago Bruce Therrien

Thanks Ian... But how can I specify a default value for a field that is primary and auto-increment, such as the customer_id field?

 
3 years, 4 months ago Ian Gilfillan

Primary key auto-increments can be made null:

ALTER TABLE customer MODIFY customer_id INT(11) NULL AUTO_INCREMENT;

but not given a DEFAULT value, as the AUTO_INCREMENT takes care of that:

ALTER TABLE customer MODIFY customer_id INT(11) NOT NULL DEFAULT '0' AUTO_INCREMENT;
ERROR 1067 (42000): Invalid default value for 'customer_id'

You shouldn't get the "Field 'xxxx' doesn't have a default value" error on an auto-increment, so if you are, it would help to find the queries resulting in the problem as well as the table state at the time of these errors.

 
Content reproduced on this site is the property of its respective owners, and this content is not reviewed in advance by MariaDB. The views, information and opinions expressed by this content do not necessarily represent those of MariaDB or any other party.