Quantcast
Channel: User spraff - Stack Overflow
Viewing all articles
Browse latest Browse all 55

unexpected implicit "DEFAULT NULL" on non-null columns when `INSERT IGNORE` is used

$
0
0

I just ran this on MySql 8 using InnoDB

CREATE TABLE TuneName(        id          BIGINT        NOT NULL AUTO_INCREMENT PRIMARY KEY,        tune_id     BIGINT        NOT NULL,        name        VARCHAR (100) NOT NULL,        norm        VARCHAR (100) NOT NULL,        base_weight FLOAT         NOT NULL DEFAULT 1,        weight      FLOAT         NOT NULL DEFAULT 1,        UNIQUE (tune_id, name),        FOREIGN KEY (tune_id) REFERENCES Tune (id)                ON DELETE CASCADE);

I am surprised to discover that name and norm are implicitly DEFAULT NULL, as shown by show columns from TuneName:

+-------------+--------------+------+-----+---------+----------------+| Field       | Type         | Null | Key | Default | Extra          |+-------------+--------------+------+-----+---------+----------------+| id          | bigint       | NO   | PRI | NULL    | auto_increment || tune_id     | bigint       | NO   | MUL | NULL    |                || name        | varchar(100) | NO   |     | NULL    |                || norm        | varchar(100) | NO   |     | NULL    |                || base_weight | float        | NO   |     | 1       |                || weight      | float        | NO   |     | 1       |                |+-------------+--------------+------+-----+---------+----------------+

although they are quite explicitly NOT NULL in the schema

When I INSERT I get an expected error

mysql> INSERT INTO TuneName ( tune_id, name, weight, base_weight ) VALUES (99, 'foo',1.0, 1.0);ERROR 1364 (HY000): Field 'norm' doesn't have a default value

however with INSERT IGNORE

mysql> INSERT IGNORE INTO TuneName ( tune_id, name, weight, base_weight ) VALUES (99, 'foo',1.0, 1.0);Query OK, 1 row affected, 1 warning (0.00 sec)mysql> select * from TuneName;+----+---------+------+------+-------------+--------+| id | tune_id | name | norm | base_weight | weight |+----+---------+------+------+-------------+--------+|  1 |      99 | foo  |      |           1 |      1 |+----+---------+------+------+-------------+--------+

I want INSERT IGNORE to not-insert if the UNIQUE(tune_id,name) constraint fails, which is why that exists, I do not want it to succeed if norm is missing.

Can I enforce that in the schema?


Viewing all articles
Browse latest Browse all 55

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>