Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I insert NULL value into column after MySQL upgrade?

Tags:

php

mysql

After upgrade my mysql I have problem with database. In logs I can see:

PHP Fatal error: Uncaught PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'kod_tecdoc' cannot be null in.

when I tried insert or update row in table. I'm suprised becouse before upgrading works good. My table fields allows to empty values. I know that solution may be

if ($kod_tecdoc == null) $kod_tecdoc = '';

but it doesn't resolve problem. Maybe some settings should be changed in mysql ?
my mysql version:

+-----------------+
| @@version       |
+-----------------+
| 5.7.12-0ubuntu1 |
+-----------------+

Table structure

 CREATE TABLE `Realizacje` (
      `id` int(11) NOT NULL,
      `tekst_allegro` text NOT NULL,
      `typy_felg` int(11) NOT NULL,
      `nazwa` varchar(200) DEFAULT NULL,
      `nazwa_allegro` varchar(255) NOT NULL,
      `producent` int(11) NOT NULL DEFAULT '0',
      `cena_netto_gielda` decimal(12,2) NOT NULL,
      `cena_brutto_gielda` decimal(12,2) NOT NULL,
      `cena_netto_hurt` decimal(12,2) NOT NULL,
      `cena_brutto_hurt` decimal(12,2) NOT NULL,
      `cena_netto_warsztat` decimal(12,2) NOT NULL,
      `cena_brutto_warsztat` decimal(12,2) NOT NULL,
      `vat` int(11) NOT NULL,
      `kod_produkt` varchar(255) NOT NULL,
      `on_pojazdy` varchar(1) NOT NULL DEFAULT '0',
      `on_zamienniki` varchar(1) NOT NULL DEFAULT '0',
      `on_kategoria` varchar(1) NOT NULL DEFAULT '0',
      `kod` varchar(255) NOT NULL,
      `kod_dostawcy` varchar(50) NOT NULL,
      `kod_tecdoc` varchar(50) NOT NULL,
      `widoczny` varchar(2) NOT NULL DEFAULT '1',
      `dostepny` varchar(2) NOT NULL DEFAULT '1',
      `dostepnosc` int(11) NOT NULL,
      `promocja` varchar(2) NOT NULL DEFAULT '0',
      `promocja_glowna` tinyint(1) NOT NULL DEFAULT '0',
      `nowosc` varchar(2) NOT NULL DEFAULT '0',
      `nowosc_glowna` tinyint(1) NOT NULL DEFAULT '1',
      `glowna` varchar(1) NOT NULL,
      `bestseller` varchar(1) DEFAULT '0',
      `bestseller_glowna` tinyint(1) NOT NULL DEFAULT '0',
      `polecamy` varchar(1) NOT NULL DEFAULT '0',
      `polecamy_glowna` varchar(1) NOT NULL DEFAULT '0',
      `skrot` text NOT NULL,
      `tekst` text,
      `odwiedziny` bigint(20) DEFAULT '0',
      `lang` varchar(5) NOT NULL DEFAULT 'pl',
      `status` int(11) DEFAULT '0',
      `link` varchar(255) DEFAULT NULL,
      `route_id` int(11) DEFAULT NULL,
      `miary` int(11) NOT NULL,
      `rabat` decimal(12,2) NOT NULL,
      `narzut` varchar(1) DEFAULT '0',
      `aukcje` varchar(255) NOT NULL,
      `stan` decimal(12,2) NOT NULL DEFAULT '1.00',
      `aktualizowany` varchar(1) NOT NULL DEFAULT '1',
      `dostawanatelefon` varchar(1) NOT NULL,
      `dodano` varchar(50) DEFAULT NULL,
      `aktualizacja` varchar(50) DEFAULT NULL,
      `kosztyAllegro` text NOT NULL,
      `allegroCennik` int(11) NOT NULL,
      `trybCena` varchar(255) NOT NULL,
      `allegro_ok` varchar(1) NOT NULL DEFAULT '0',
      `ogranicz_ilosc_znakow` int(11) NOT NULL DEFAULT '50'
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
like image 738
Tomasz Avatar asked Dec 10 '22 17:12

Tomasz


2 Answers

Your local MySQL server is running in "strict" mode, which throws an error in case like yours, and several others that are otherwise handled "gracefully" without strict mode. As of MySQL 5.7.5, the default SQL mode includes STRICT_TRANS_TABLES, which is exactly the cause of your headache.

To change your local server's mode, edit your my.cnf (or my.ini if you're running Windows) and then set the mode to:

sql-mode=""

Or, upon connecting to MySQL in your web app, execute this query first right after the connection is estabilished:

SET GLOBAL sql_mode = '';

I'd advise against this because it lets you execute a bit careless statements. In the long run, you're better off with adjusting your schema instead.

like image 103
Oliver Maksimovic Avatar answered Feb 24 '23 13:02

Oliver Maksimovic


My table fields allows to empty values.

That's not true:

`kod_tecdoc` varchar(50) NOT NULL,
                         ^^^^^^^^

If the column is really optional, alter the table and make it NULLable.

like image 44
Álvaro González Avatar answered Feb 24 '23 14:02

Álvaro González