简体   繁体   English

字段 'user_id' 没有默认值,但设置为 auto_increment

[英]Field 'user_id' doesn't have a default value, but is set to auto_increment

So I'm trying to build a meal planner program, but when I try to submit a username it gives me the error displayed in the title.所以我正在尝试构建一个膳食计划程序,但是当我尝试提交用户名时,它给了我标题中显示的错误。 I've tried adding additional mapping to my classes, I created a new repo for the User class, and I'm not sure why the program isn't auto-incrementing the user_ID value like it should.我已经尝试向我的类添加额外的映射,我为用户 class 创建了一个新的存储库,但我不确定为什么程序没有像它应该的那样自动增加 user_ID 值。

Here is my offending mySQL table:这是我的违规 mySQL 表:

CREATE TABLE `user_table` (
  `user_id` int NOT NULL AUTO_INCREMENT,
  `user_name` varchar(30),
  PRIMARY KEY (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
/*!40101 SET character_set_client = @saved_cs_client */;

Here is the User class:这是用户 class:

@Entity
@Table(name="user_table")
public class User {

    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    @Column(name="user_ID")
    private int userID;

    @Column(name="user_name")
    private String userName;

    public User() {
        super();

        // TODO Auto-generated constructor stub
    }

    public User(int userID, String userName) {
        super();
        this.userID = userID;
        this.userName = userName;
    }

    public int getUserID() {
        return userID;
    }
    public void setUserID(int userID) {
        this.userID = userID;
    }
    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    @Override
    public String toString() {

        return "User [userID=" + userID + ", userName=" + userName + "]";
    }
}

The repo the user class attaches to:用户 class 的存储库附加到:

@Repository
public interface MealPlannerRepositoryUser extends JpaRepository<User, Long>{

}

And the relevant methods from my controller class:以及来自我的controller class的相关方法:

    @Autowired
    MealPlannerRepository repo;

    @Autowired
    MealPlannerRepositoryUser userRepo;

    @GetMapping("/inputUser")
    public String addNewUser(Model model) {
        User u = new User();
        Meal m = new Meal();
        model.addAttribute("newUser", u);
        model.addAttribute("newMeal", m);
        return "inputUser";
    }

    @PostMapping("/inputUser")
    public String addNewUser(@ModelAttribute User u, Meal m, Model model) {
        userRepo.save(u);
        repo.save(m);
        model.addAttribute("newUser", repo.findAll());
        model.addAttribute("newMeal", repo.findAll());
        return "results";
    }

I must be missing something somewhere, but I'm out of ideas.我一定在某个地方遗漏了一些东西,但我没有想法。

I am not sure if this is case sensitive or not but the annotation @Column('user_ID') in the User class doesn't match with the column name 'user_id' in your SQL query.我不确定这是否区分大小写,但用户 class 中的注释 @Column('user_ID') 与 SQL 查询中的列名 'user_id' 不匹配。

in the entity class user_ID doesn't match with column name user_id, change in entity class user_id在实体 class 中,user_ID 与列名 user_id 不匹配,在实体 class 中更改 user_id

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

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