Ruby ruby 3.2 代码脚手架生成的 sql 默认主键为空导致报错有人知道怎么设置主键为非空么

18234105447 · June 08, 2022 · Last by spike76 replied at June 08, 2022 · 220 hits

== CreatePosts: migrating ==================================================== -- create_table(:posts) rake aborted! StandardError: An error has occurred, all later migrations canceled:

Mysql2::Error: All parts of a PRIMARY KEY must be NOT NULL; if you need NULL in a key, use UNIQUE instead: CREATE TABLE posts (id int(11) DEFAULT NULL auto_increment PRIMARY KEY, name varchar(255), title varchar(255), content text, created_at datetime NOT NULL, updated_at datetime NOT NULL) ENGINE=InnoDB /usr/share/rvm/gems/ruby-2.1.6/gems/mysql2-0.3.23/lib/mysql2/client.rb:80:in _query' /usr/share/rvm/gems/ruby-2.1.6/gems/mysql2-0.3.23/lib/mysql2/client.rb:80:inblock in query' /usr/share/rvm/gems/ruby-2.1.6/gems/mysql2-0.3.23/lib/mysql2/client.rb:79:in handle_interrupt' /usr/share/rvm/gems/ruby-2.1.6/gems/mysql2-0.3.23/lib/mysql2/client.rb:79:inquery'

ruby 是 2.1.6 rails 是 3.2.22.2 生成 model 语句为 demo 实例:rails generate scaffold Post name:string title:string content:text

快十年前的版本了哦。。。

手动改改 migration 文件应该就行。不知道为啥 3.2 的版本生成的主键会 default null

Reply to spike76

老项目 没办法 具体应该怎么添加呢 这个主键 id 是系统自动生成的 我在属性里面都没定义

You need to Sign in before reply, if you don't have an account, please Sign up first.