简体   繁体   English

字段“id”没有默认值?

[英]Field 'id' doesn't have a default value?

I am new to this SQL;我是这个 SQL 的新手; I have seen similar question with much bigger programs, which I can't understand at the moment.我在更大的程序中看到了类似的问题,目前我无法理解。 I am making a database for games of cards to use in my homepage.我正在为我的主页中使用的纸牌游戏创建一个数据库。

I am using MySQL Workbench on Windows.我在 Windows 上使用 MySQL Workbench。 The error I get is:我得到的错误是:

Error Code: 1364. Field 'id' doesn't have a default value错误代码:1364。字段“id”没有默认值

CREATE TABLE card_games
(
nafnleiks varchar(50), 
leiklysing varchar(3000), 
prentadi varchar(1500), 
notkunarheimildir varchar(1000), 
upplysingar varchar(1000), 
ymislegt varchar(500), 
id int(11) PK
);

insert into card_games (nafnleiks, leiklysing, prentadi, notkunarheimildir, upplysingar, ymislegt)

values('Svartipétur',
'Leiklýsingu vantar',
'Er prentað í: Þórarinn Guðmundsson (2010). Spilabókin - Allir helstu spilaleikir og spil.',
'Heimildir um notkun: Árni Sigurðsson (1951). Hátíðir og skemmtanir fyrir hundrað árum',
'Aðrar upplýsingar',
'ekkert hér sem stendur'
);

values('Handkurra',
'Leiklýsingu vantar',
'Er prentað í: Þórarinn Guðmundsson (2010). Spilabókin - Allir helstu spilaleikir og spil.',
'Heimildir um notkun', 
'Aðrar upplýsingar',
'ekkert her sem stendur'
);

values('Veiðimaður',
'Leiklýsingu vantar',
'Þórarinn Guðmundsson (2010). Spilabókin - Allir helstu spilaleikir og spil. Reykjavík: Bókafélagið. Bls. 19-20.',
'vantar',
'vantar',
'vantar'
);

As id is the primary key, you cannot have different rows with the same value.由于id是主键,因此不能有具有相同值的不同行。 Try to change your table so that the id is auto incremented:尝试更改您的表,以便id自动递增:

id int NOT NULL AUTO_INCREMENT

and then set the primary key as follows:然后设置主键如下:

PRIMARY KEY (id)

All together:全部一起:

CREATE TABLE card_games (
   id int(11) NOT NULL AUTO_INCREMENT,
   nafnleiks varchar(50),
   leiklysing varchar(3000), 
   prentadi varchar(1500), 
   notkunarheimildir varchar(1000),
   upplysingar varchar(1000),
   ymislegt varchar(500),
   PRIMARY KEY (id));

Otherwise, you can indicate the id in every insertion, taking care to set a different value every time:否则,您可以在每次插入时指定id ,每次都要注意设置不同的值:

insert into card_games (id, nafnleiks, leiklysing, prentadi, notkunarheimildir, upplysingar, ymislegt)

values(1, 'Svartipétur', 'Leiklýsingu vantar', 'Er prentað í: Þórarinn Guðmundsson (2010). Spilabókin - Allir helstu spilaleikir og spil.', 'Heimildir um notkun: Árni Sigurðsson (1951). Hátíðir og skemmtanir fyrir hundrað árum', 'Aðrar upplýsingar', 'ekkert hér sem stendur' );

There are 2 solutions mentioned below:下面提到了2个解决方案:

Solution 1解决方案 1

MySQL is most likely in STRICT SQL mode . MySQL 很可能处于STRICT SQL 模式 Try to execute SQL query SET GLOBAL sql_mode='' or edit your my.cnf / my.ini to make sure you aren't setting STRICT_ALL_TABLES and/or STRICT_TRANS_TABLES .尝试执行 SQL 查询SET GLOBAL sql_mode=''或编辑您的 my.cnf / my.ini 以确保您没有设置STRICT_ALL_TABLES和/或STRICT_TRANS_TABLES

Solution 2解决方案 2

If Solution-1 is not working then try Solution-2 as given in below steps:如果解决方案 1 不起作用,请按照以下步骤尝试解决方案 2:

  1. Run MySQL Administrator tool as Administrator.以管理员身份运行MySQL 管理员工具
  2. Then go to Startup Variable.然后转到启动变量。
  3. Then go to the Advance tab.然后转到高级选项卡。
  4. find SQL Mode and remove the STRICT_ALL_TABLES and/or STRICT_TRANS_TABLES and then Click on Apply Changes.找到 SQL 模式并删除STRICT_ALL_TABLES和/或STRICT_TRANS_TABLES ,然后单击 Apply Changes。
  5. Restart MySQL Server.重启 MySQL 服务器。
  6. Done.完毕。

Note: I have tested these solutions in MySQL Server 5.7注意:我已经在 MySQL Server 5.7 中测试过这些解决方案

The id should set as auto-increment . id 应设置为auto-increment

To modify an existing id column to auto-increment, just add this要将现有的 id 列修改为自动递增,只需添加此

ALTER TABLE card_games MODIFY id int NOT NULL AUTO_INCREMENT;

I was getting error while ExecuteNonQuery() resolved with adding AutoIncrement to Primary Key of my table.当 ExecuteNonQuery() 通过将 AutoIncrement 添加到我的表的主键来解决时,我遇到了错误。 In your case if you don't want to add primary key then we must need to assign value to primary key.在您的情况下,如果您不想添加主键,那么我们必须需要为主键赋值。

ALTER TABLE `t1` 
CHANGE COLUMN `id` `id` INT(11) NOT NULL AUTO_INCREMENT ;

Since mysql 5.6, there is a new default that makes sure you are explicitly inserting every field that doesn't have a default value set in the table definition.从 mysql 5.6 开始,有一个新的默认值可确保您明确插入在表定义中没有设置默认值的每个字段。

to disable and test this: see this answer here: mysql error 1364 Field doesn't have a default values禁用和测试这个:在这里看到这个答案: mysql error 1364 Field doesn't have a default values

I would recommend you test without it, then reenable it and make sure all your tables have default values for fields you are not explicitly passing in every INSERT query.我建议您在没有它的情况下进行测试,然后重新启用它并确保您的所有表都具有您未在每个 INSERT 查询中显式传递的字段的默认值。

If a third party mysql viewer is giving this error, you are probably limited to the fix in that link.如果第三方 mysql 查看器出现此错误,您可能仅限于该链接中的修复程序。

This is caused by MySQL having a strict mode set which won't allow INSERT or UPDATE commands with empty fields where the schema doesn't have a default value set.这是由于 MySQL 设置了严格的模式,该模式不允许 INSERT 或 UPDATE 命令具有空字段,而模式没有设置默认值。

There are a couple of fixes for this.对此有几个修复。

First 'fix' is to assign a default value to your schema.第一个“修复”是为您的架构分配一个默认值。 This can be done with a simple ALTER command:这可以通过一个简单的 ALTER 命令来完成:

ALTER TABLE `details` CHANGE COLUMN `delivery_address_id` `delivery_address_id` INT(11) NOT NULL DEFAULT 0 ;

However, this may need doing for many tables in your database schema which will become tedious very quickly.但是,这可能需要对数据库模式中的许多表执行此操作,这将很快变得乏味。 The second fix is to remove sql_mode STRICT_TRANS_TABLES on the mysql server.第二个修复是删除 mysql 服务器上的 sql_mode STRICT_TRANS_TABLES。

If you are using a brew installed MySQL you should edit the my.cnf file in the MySQL directory.如果您使用的是 brew 安装的 MySQL,您应该编辑 MySQL 目录中的 my.cnf 文件。 Change the sql_mode at the bottom:更改底部的 sql_mode:

#sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
sql_mode=NO_ENGINE_SUBSTITUTION

Save the file and restart Mysql.保存文件并重新启动 Mysql。

Source: https://www.euperia.com/development/mysql-fix-field-doesnt-default-value/1509来源: https ://www.euperia.com/development/mysql-fix-field-doesnt-default-value/1509

make sure that you do not have defined setter for the for your primary key in model class.确保您没有为模型类中的主键定义 setter。

public class User{
@Id
@GeneratedValues
private int user_Id;
private String userName;

public int getUser_Id{
return user_Id;
}

public String getUserName{
return userName;
}

public void setUserName{
this.userName=userName;
}
}

To detect run:要检测运行:

select @@sql_mode
-- It will give something like:
-- STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

To fix, run:要修复,请运行:

set global sql_mode = ''

if you add the AUTO_INCREMENT clause to the primary key id field in the database table as shown below it will work.如果您将 AUTO_INCREMENT 子句添加到数据库表中的主键 id 字段,如下所示,它将起作用。

CREATE TABLE user_role ( user_role_id bigint NOT NULL AUTO_INCREMENT, user_id bigint NOT NULL, role_id bigint NOT NULL,创建表user_role ( user_role_id bigint NOT NULL AUTO_INCREMENT, user_id bigint NOT NULL, role_id bigint NOT NULL,

Solution: Remove STRICT_TRANS_TABLES from sql_mode解决方案:从sql_mode删除STRICT_TRANS_TABLES

To check your default setting,要检查您的默认设置,

mysql> set @@sql_mode = 
'STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
Query OK, 0 rows affected (0.00 sec)

mysql> select @@sql_mode;
+----------------------------------------------------------------+
| @@sql_mode                                                     |
+----------------------------------------------------------------+
| STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
+----------------------------------------------------------------+
1 row in set (0.00 sec)

Run a sample query运行示例查询

mysql> INSERT INTO nb (id) VALUES(3);
ERROR 1364 (HY000): Field 'field' doesn't have a default value

Remove your STRICT_TRANS_TABLES by resetting it to null.通过将其重置为空来删除您的STRICT_TRANS_TABLES

mysql> set @@sql_mode = '';
Query OK, 0 rows affected (0.00 sec)

Now, run the same test query.现在,运行相同的测试查询。

mysql> INSERT INTO nb (id) VALUES(3);
Query OK, 1 row affected, 1 warning (0.00 sec)

Source: https://netbeans.org/bugzilla/show_bug.cgi?id=190731来源: https ://netbeans.org/bugzilla/show_bug.cgi?id=190731

For me the issue got fixed when I changed对我来说,当我改变时问题得到了解决

<id name="personID" column="person_id">
    <generator class="native"/>
</id>

to

<id name="personID" column="person_id">
    <generator class="increment"/>
</id>

in my Person.hbm.xml .在我的Person.hbm.xml中。

after that I re-encountered that same error for an another field(mobno).之后,我再次遇到另一个字段(mobno)的相同错误。 I tried restarting my IDE, recreating the database with previous back issue got eventually fixed when I re-create my tables using (without ENGINE=InnoDB DEFAULT CHARSET=latin1; and removing underscores in the field name)我尝试重新启动我的 IDE,当我使用重新创建表时,使用先前的问题重新创建数据库最终得到修复(没有ENGINE=InnoDB DEFAULT CHARSET=latin1;并删除字段名称中的下划线)

CREATE TABLE `tbl_customers` (
  `pid` bigint(20) NOT NULL,
  `title` varchar(4) NOT NULL,
  `dob` varchar(10) NOT NULL,
  `address` varchar(100) NOT NULL,
  `country` varchar(4) DEFAULT NULL,
  `hometp` int(12) NOT NULL,
  `worktp` int(12) NOT NULL,
  `mobno` varchar(12) NOT NULL,
  `btcfrom` varchar(8) NOT NULL,
  `btcto` varchar(8) NOT NULL,
  `mmname` varchar(20) NOT NULL
)

instead of代替

CREATE TABLE `tbl_person` (
  `person_id` bigint(20) NOT NULL,
  `person_nic` int(10) NOT NULL,
  `first_name` varchar(20) NOT NULL,
  `sur_name` varchar(20) NOT NULL,
  `person_email` varchar(20) NOT NULL,
  `person_password` varchar(512) NOT NULL,
  `mobno` varchar(10) NOT NULL DEFAULT '1',
  `role` varchar(10) NOT NULL,
  `verified` int(1) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

I probably think this due to using ENGINE=InnoDB DEFAULT CHARSET=latin1;我可能认为这是由于使用ENGINE=InnoDB DEFAULT CHARSET=latin1; , because I once got the error org.hibernate.engine.jdbc.spi.SqlExceptionHelper - Unknown column 'mob_no' in 'field list' even though it was my previous column name, which even do not exist in my current table. ,因为我曾经收到错误org.hibernate.engine.jdbc.spi.SqlExceptionHelper - Unknown column 'mob_no' in 'field list'即使它是我以前的列名,甚至在我当前的表中也不存在。 Even after backing up the database(with modified column name, using InnoDB engine) I still got that same error with old field name.即使在备份数据库(修改列名,使用 InnoDB 引擎)之后,我仍然遇到与旧字段名相同的错误。 This probably due to caching in that Engine.这可能是由于该引擎中的缓存。

I landed this question in 2019. MY problem was updating table1 with table2 ignoring the variables with different name in both tables.我在 2019 年提出了这个问题。我的问题是用 table2 更新 table1 忽略两个表中具有不同名称的变量。 I was getting the same error as mentioned in question: Error Code: 1364. Field 'id' doesn't have a default value in mysql.我遇到了与问题中提到的相同的错误:错误代码:1364。字段'id'在mysql中没有默认值 Here is how solved it:以下是解决方法:

Table 1 Schema : id ( unique & auto increment)|表 1 Schema : id (unique & auto increment)| name |姓名 | profile |简介 | Age Table 2 Schema: motherage|年龄表2 Schema:motherage| father|父亲| name|姓名| profile轮廓

This solved my error: INSERT IGNORE INTO table2 (name,profile) SELECT name, profile FROM table1这解决了我的错误: INSERT IGNORE INTO table2 (name,profile) SELECT name, profile FROM table1

Disable FOREIGN_KEY_CHECKS and then禁用 FOREIGN_KEY_CHECKS 然后

`SET FOREIGN_KEY_CHECKS = 0; `SET FOREIGN_KEY_CHECKS = 0;

ALTER TABLE card_games MODIFY COLUMN id INT NOT NULL AUTO_INCREMENT; ALTER TABLE card_games MODIFY COLUMN id INT NOT NULL AUTO_INCREMENT;

SET FOREIGN_KEY_CHECKS = 1;`设置 FOREIGN_KEY_CHECKS = 1;`

As a developer it is highly recommended to use STRICT mode because it will allow you to see issues/errors/warnings that may come about instead of just working around it by turning off strict mode.作为开发人员,强烈建议使用STRICT模式,因为它可以让您查看可能出现的问题/错误/警告,而不仅仅是通过关闭严格模式来解决它。 It's also better practice.这也是更好的做法。

Strict mode is a great tool to see messy, sloppy code.严格模式是查看凌乱、草率代码的好工具。

I had an issue on AWS with mariadb - This is how I solved the STRICT_TRANS_TABLES issue我在 AWS 上遇到了 mariadb 的问题 - 这就是我解决 STRICT_TRANS_TABLES 问题的方法

SSH into server and chanege to the ect directory SSH 进入服务器并切换到 ect 目录

[ec2-user]$ cd /etc

Make a back up of my.cnf备份 my.cnf

[ec2-user etc]$ sudo cp -a my.cnf{,.strict.bak}

I use nano to edit but there are others我使用 nano 进行编辑,但还有其他的

[ec2-user etc]$ sudo nano my.cnf

Add this line in the the my.cnf file在 my.cnf 文件中添加这一行

#
#This removes STRICT_TRANS_TABLES
#
sql_mode=""

Then exit and save然后退出并保存

OR if sql_mode is there something like this:或者如果 sql_mode 有这样的东西:

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

Change to改成

sql_mode=""

exit and save退出并保存

then restart the database然后重启数据库

[ec2-user etc]$ sudo systemctl restart mariadb

It amazing for me, for solving Field 'id' doesn't have a default value?对我来说太神奇了,解决Field 'id' 没有默认值? I have tried all the possible ways what are given here like..我已经尝试了这里给出的所有可能的方法,比如..

set sql_mode=""
set @@sql_mode = ''; etc

but unfortunately these didn't work for me.但不幸的是,这些对我不起作用。 So after long investigation, I found that所以经过长时间的调查,我发现

@Entity
@Table(name="vendor_table")
public class Customer {
    @Id
    @Column(name="cid")
    private int cid;
.....
}

@Entity
@Table(name="vendor_table")
public class Vendor {
    @Id
    private int vid;
    @Column
    private String vname;
    .....
}

here you can see that both tables are having same name.在这里您可以看到两个表具有相同的名称。 It is very funny mistake, was done by me :)))).这是一个非常有趣的错误,是我做的:))))。 After correcting this,my problem was gone off.改正后,我的问题就解决了。

If you are creating your schema using MySQL Workbench, you can check this checkbox.如果您使用 MySQL Workbench 创建模式,则可以选中此复选框。

在此处输入图像描述

If you can't see the image, it's the column titled AI, if you hover over it you'll see it's label Mark column as AUTO_INCREMENT .如果您看不到图像,则它是标题为 AI 的列,如果您将鼠标悬停在它上面,您会看到它的标签Mark column as AUTO_INCREMENT

为您的 Id 添加一个默认值让我们在您的表定义中说这将解决您的问题。

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

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