简体   繁体   中英

MySQL - Field 'id' doesn't have a default value

I can't insert any rows into my database, and the error I'm getting is:

This doesn't make any sense as it was working fine before, nothing has changed as far as I know. The ID field is set to primary, not null and auto increment: 在此处输入图片说明

I was able to insert a row manually through PHPMyAdmin. Heres the code I'm inserting with:

    $query = "INSERT INTO wp_genomics_results (file_id,snp_id,genotype,reputation,zygosity) VALUES (?,?,?,?,?)";

    $stmt = $ngdb->prepare($query);

    $file_id = $this->fileID;
    $snp_id = $row['id'];
    $genotype = $this->formatGenotype($extractedGenotype);
    $zygosity = $this->assignZygosity($genotype,$minor_allele);
    $reputation = $this->assignReputation($zygosity,$genotypes);

    $stmt->bind_param("sssss", $file_id,$snp_id,$genotype,$reputation,$zygosity);
    $stmt->execute();

I tried deleting the table and creating a new table but it didn't fix it. Is there anything else I can do to try and diagnose and fix this?

make sure for this line:

$stmt->bind_param("sssss", $file_id,$snp_id,$genotype,$reputation,$zygosity);

First parameter of bind_param() you used is "sssss" that means all off data is string. Please fix it as the right type data of your type on table database. Please use "i" for integer. For example:

$stmt->bind_param("iisss", $file_id,$snp_id,$genotype,$reputation,$zygosity);

I make the code above as an example because I can not see all of your table descibe.

EDIT

The table definition looks really strange. It looks like there's a second index on the id column. The id column is already defined as the PRIMARY KEY .
It's this line in the table definition that is bothers me...

  KEY `id` (`id`)

It's bizarre that MySQL would even let you add a second index on the same column, and not return an error like "such an index already exist" or "the set of columns is already indexed" or such.

I recommend you run a statement to drop the index named id . Execute either

  DROP INDEX `id` ON `wp_genomics_results`;

-or-

  ALTER TABLE `wp_genomics_results` DROP KEY `id`;  

It's possible that this index was contributing to the issue.


ORIGINAL ANSWER

I suggest you figure out exactly which statement is throwing an error.

I suspect it's not the execution of INSERT INTO wp_genomics_results statement.

If it were me, I'd take a look at the assignZygosity and assignReputation functions. I suspect the culprit is in down in there.

I suspect there's something going on in those functions. My guess (just a guess) is that one of those functions is performing a lookup to get a value for a foreign key. And, if a foreign key value isn't found, something is being executed to INSERT a row into the referenced table. There must be some reason that function is being called, some reason we're not just using the value supplied as a parameter...

The formatGenotype function is also a suspect, by proximity. (I'm less a-feared of that function name... but again, I have no idea what that function is doing.)

If that's not the issue, if it's really the execution of the INSERT statement shown in the code, then I'd look for a "BEFORE INSERT" trigger on the wp_genomics_results table... maybe the trigger is performing an insert to some other table.


As a quick test, I'd comment out the calls to those functions, and just assign a literal values to $zygosity and $reputation .

I want to know the exact line number in the exact file where the error is being thrown.

I also want the complete error message from MySQL.

Following every execution of a database interaction, I want to check the return, and if there's an error, I want (for debugging purposes) a unique message that indicates where we are in the code when the error is thrown, along with the complete error message returned from MySQL ( mysqli_error ).

I'm not going to just assume, without evidence, that it's the execution of this particular statement that's causing the error.

And if it really is this statement, then we'd need to take a look at the actual MySQL definition of the table... the output from a SHOW CREATE TABLE wp_genomics_results statement.

And we want to make sure that we are looking at the same MySQL instance and database that the code is connecting to, and not some other MySQL instance or database.

I tried inserting with the WordPress database connection:

$wpdb->query("INSERT INTO wp_genomics_results (file_id,snp_id,genotype,reputation,zygosity) VALUES ('$file_id','$snp_id','$genotype','$reputation','$zygosity')");

and it works, but if I try using an identical query with my own database connection:

$db = new mysqli('localhost', NG_DB_USER, NG_DB_PASS, NG_DB_NAME);
$db->query("INSERT INTO wp_genomics_results (file_id,snp_id,genotype,reputation,zygosity) VALUES ('111','$snp_id','$genotype','$reputation','$zygosity')");

I get the same error:

Error No: 1364 - MySQL error Field 'id' doesn't have a default value

I don't understand why it works with wordpress but not my own database connection. I went into my mysql.ini file and changed this:

sql-mode="STRICT_ALL_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_ZERO_DATE, NO_ZERO_IN_DATE,NO_AUTO_CREATE_USER"

to this:

sql-mode=""

and now I can insert a row, the error stopped happening. I still don't understand why this error was occuring to begin with. And now theres a new error happening:

Duplicate entry '0' for key 'PRIMARY'

I don't understand it because the id column is set to auto increment. Theres also no rows in there with ID = 0. I even emptied the table to make absolute sure there is no row in there with ID = 0. I'm very confused about whats happening. Heres the output of the table structure:

> mysql> SHOW CREATE TABLE wp_genomics_results;

| wp_genomics_results | CREATE TABLE `wp_genomics_results` (
   `id` bigint(11) NOT NULL AUTO_INCREMENT,
   `file_id` int(11) NOT NULL,
   `snp_id` int(11) NOT NULL,
   `genotype` varchar(3) NOT NULL,
   `reputation` varchar(3) NOT NULL,
   `zygosity` int(3) NOT NULL,
    PRIMARY KEY (`id`),
    KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+---------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)

I notice it says 1 row in set, but the table is actually empty. And once again, I'm able to insert a new row through PHPMyAdmin: 在此处输入图片说明

Oh God I feel like an idiot. All this time, the problem was that I had the wrong database selected. So it was querying a table from a different database, and this tables ID field didn't have auto increment set. Ah well, it was a good learning experience, getting to know MySQL better.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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