简体   繁体   English

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

[英]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: ID 字段设置为主,不为空和自动递增: 在此处输入图片说明

I was able to insert a row manually through PHPMyAdmin.我能够通过 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); $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.您使用的 bind_param() 的第一个参数是“sssss”,这意味着所有关闭数据都是字符串。 Please fix it as the right type data of your type on table database.请将其修复为表数据库上您类型的正确类型数据。 Please use "i" for integer.请使用“i”表示整数。 For example:例如:

$stmt->bind_param("iisss", $file_id,$snp_id,$genotype,$reputation,$zygosity); $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.看起来id列上有第二个索引。 The id column is already defined as the PRIMARY KEY . id列已定义为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.奇怪的是,MySQL 甚至允许您在同一列上添加第二个索引,并且不会返回诸如“这样的索引已经存在”或“列集已经建立索引”之类的错误。

I recommend you run a statement to drop the index named id .我建议您运行一个语句来删除名为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.我怀疑这不是INSERT INTO wp_genomics_results语句的执行。

If it were me, I'd take a look at the assignZygosity and assignReputation functions.如果是我,我会看看assignZygosityassignReputation函数。 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. formatGenotype函数也是一个可疑的对象。 (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.如果这不是问题,如果它真的是代码中显示的 INSERT 语句的执行,那么我会在wp_genomics_results表上寻找“BEFORE INSERT”触发器......也许触发器正在执行对其他表的插入.


As a quick test, I'd comment out the calls to those functions, and just assign a literal values to $zygosity and $reputation .作为一个快速测试,我会注释掉对这些函数的调用,并只为$zygosity$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.我还想要来自 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 ).每次执行数据库交互之后,我想检查返回,如果有错误,我想要(出于调试目的)一个唯一的消息,指示抛出错误时我们在代码中的位置,以及完整的错误从 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.如果真的是这个语句,那么我们需要看看表的实际 MySQL 定义...... SHOW CREATE TABLE wp_genomics_results语句的输出。

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.并且我们希望确保我们正在查看代码连接到的同一个MySQL 实例和数据库,而不是其他某个 MySQL 实例或数据库。

I tried inserting with the WordPress database connection:我尝试使用 WordPress 数据库连接插入:

$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错误编号:1364 - MySQL 错误字段“id”没有默认值

I don't understand why it works with wordpress but not my own database connection.我不明白为什么它适用于 wordpress 而不是我自己的数据库连接。 I went into my mysql.ini file and changed this:我进入了我的 mysql.ini 文件并改变了这个:

sql-mode="STRICT_ALL_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_ZERO_DATE, NO_ZERO_IN_DATE,NO_AUTO_CREATE_USER" 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="" 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'键“P​​RIMARY”的重复条目“0”

I don't understand it because the id column is set to auto increment.我不明白,因为 id 列设置为自动递增。 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.那里也没有 ID = 0 的行。我什至清空了表以确保绝对确定那里没有 ID = 0 的行。我对发生的事情感到非常困惑。 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.我注意到它说的是 1 行,但表实际上是空的。 And once again, I'm able to insert a new row through PHPMyAdmin:再一次,我可以通过 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.所以它从不同的数据库查询一个表,并且这个表 ID 字段没有设置自动增量。 Ah well, it was a good learning experience, getting to know MySQL better.嗯,这是一次很好的学习经历,更好地了解 MySQL。

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

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