{"id":612,"date":"2018-06-10T21:21:50","date_gmt":"2018-06-10T12:21:50","guid":{"rendered":"https:\/\/yuis-programming.com\/?p=612"},"modified":"2018-05-01T01:22:00","modified_gmt":"2018-04-30T16:22:00","slug":"rails-%e3%83%a2%e3%83%87%e3%83%ab%e3%81%b8%e3%81%ae%e3%83%a6%e3%83%8b%e3%83%bc%e3%82%af%e3%82%ad%e3%83%bc%e3%82%84not-null%e3%81%ae%e4%bb%98%e3%81%91%e6%96%b9","status":"publish","type":"post","link":"https:\/\/yuis-programming.com\/?p=612","title":{"rendered":"Rails \u30e2\u30c7\u30eb\u3078\u306e\u30e6\u30cb\u30fc\u30af\u30ad\u30fc\u3084NOT NULL\u306e\u4ed8\u3051\u65b9"},"content":{"rendered":"<div id=\"toc_container\" class=\"no_bullets\"><p class=\"toc_title\">Contents<\/p><ul class=\"toc_list\"><li><a href=\"#i\"><span class=\"toc_number toc_depth_1\">1<\/span> \u30d7\u30e9\u30a4\u30de\u30ea\u30fc\u30ad\u30fc\u3068\u30e6\u30cb\u30fc\u30af\u30ad\u30fc\u306e\u9055\u3044<\/a><ul><li><a href=\"#ID\"><span class=\"toc_number toc_depth_2\">1.1<\/span> \u4e00\u610f\u306e\u30e6\u30fc\u30b6\u30fcID\u3092\u5b9f\u88c5\u3059\u308b<\/a><\/li><li><a href=\"#ID-2\"><span class=\"toc_number toc_depth_2\">1.2<\/span> \u30e6\u30fc\u30b6\u30fcID\u3092\u3069\u3046\u5b9f\u88c5\u3059\u308b\u304b<\/a><\/li><\/ul><\/li><\/ul><\/div>\n<h1><span id=\"i\">\u30d7\u30e9\u30a4\u30de\u30ea\u30fc\u30ad\u30fc\u3068\u30e6\u30cb\u30fc\u30af\u30ad\u30fc\u306e\u9055\u3044<\/span><\/h1>\n<p>PRIMARY KEY \u5236\u7d04\u306f\u3001\u3042\u308b\u30c6\u30fc\u30d6\u30eb\u306e 1\u3064\u306e\u5217\u307e\u305f\u306f\u8907\u6570\u306e\u5217\u306b1\u5ea6\u3060\u3051\u4f7f\u7528\u3067\u304d\u308b\u304c\u3001UNIQUE \u5236\u7d04\u306f\u4f55\u5ea6\u3067\u3082\u4f7f\u7528\u3067\u304d\u308b\u3002<br \/>\nPRIMARY KEY \u5236\u7d04\u3092\u6301\u3064\u5217\u306b\u306f NULL \u304c\u542b\u307e\u308c\u306a\u3044\u304c\u3001UNIQUE \u5236\u7d04\u3092\u6301\u3064\u5217\u306b\u306f NULL \u304c\u542b\u307e\u308c\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u3002<\/p>\n<h2><span id=\"ID\">\u4e00\u610f\u306e\u30e6\u30fc\u30b6\u30fcID\u3092\u5b9f\u88c5\u3059\u308b<\/span><\/h2>\n<p>\u30e6\u30fc\u30b6\u30fc\u767b\u9332\u3092\u5b9f\u88c5\u3059\u308b\u4e0a\u3067\u51fa\u3066\u304f\u308b\u8ab2\u984c\u304c\u3001\u300c\u30e6\u30fc\u30b6\u30fcID\u300d\u3067\u3059\u3002<br \/>\ntwitter\u3067\u3044\u3046\u3001<code>@fumiya_ja<\/code>\u307f\u305f\u3044\u306a\u306e\u3067\u3059\u306d\u3002<br \/>\n\u30d7\u30e9\u30a4\u30de\u30ea\u30fc\u30ad\u30fc\u306e\u4ed8\u5c5e\u3057\u305f\u30c6\u30fc\u30d6\u30eb\u306eID\u3092\u305d\u3082\u307e\u307e\u30e6\u30fc\u30b6\u30fcID\u3092\u306b\u3057\u3066\u3057\u307e\u3063\u3066\u3082\u3044\u3044\u306e\u3067\u3059\u304c\u3001(e.g.@832748 \u307f\u305f\u3044\u306aID\u306b\u306a\u308b)<br \/>\n\u305d\u3046\u3059\u308b\u3068\u3001\u30c6\u30fc\u30d6\u30eb\u306e\u30ec\u30b3\u30fc\u30c9\u306e\u6570\u3001\u3064\u307e\u308a\u30e6\u30fc\u30b6\u30fc\u6570\u304c\u4e38\u898b\u3048\u306b\u306a\u3063\u3066\u3057\u307e\u3046\u3068\u304b\u3001\u5358\u7d14\u306b\u30e6\u30fc\u30b6\u30d3\u30ea\u30c6\u30a3\u304c\u60aa\u3044\u3063\u3066\u3044\u3046\u3088\u3046\u306a\u30c7\u30e1\u30ea\u30c3\u30c8\u304c\u3042\u308a\u307e\u3059\u3002<br \/>\n\u306a\u306e\u3067\u3001\u3061\u3087\u3063\u3068\u9762\u5012\u3067\u3082\u3001\u30e6\u30fc\u30b6\u30fcID\u306f\u4e00\u610f\u306e\u30c7\u30fc\u30bf\u3068\u3057\u3066\u30e6\u30cb\u30fc\u30af\u30ad\u30fc\u3092\u3064\u3051\u3066\u3001\u5b9f\u88c5\u3059\u308b\u306e\u304c\u826f\u3044\u304b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<h2><span id=\"ID-2\">\u30e6\u30fc\u30b6\u30fcID\u3092\u3069\u3046\u5b9f\u88c5\u3059\u308b\u304b<\/span><\/h2>\n<p>\u30e6\u30cb\u30fc\u30af\u30ad\u30fc\u306fNULL\u304c\u8a31\u53ef\u3055\u308c\u3066\u3044\u308b\u306e\u3067\u3001NOT NULL\u5236\u7d04\u3092\u4ed8\u52a0\u3057\u307e\u3059\u3002<\/p>\n<ul>\n<li>\u30e6\u30cb\u30fc\u30af\u30ad\u30fc\u5236\u7d04\u3092\u3059\u308b<\/li>\n<\/ul>\n<pre><code># 1\u3064\u306e\u30ab\u30e9\u30e0\u306b\nadd_index  :keywords, :site_id , unique: true\n\n# \u8907\u6570\u306e\u30ab\u30e9\u30e0\u306b\nadd_index  :keywords, [:site_id, :name, :date], unique: true\n<\/code><\/pre>\n<ul>\n<li>NOT NULL\u5236\u7d04\u3092\u3059\u308b<\/li>\n<\/ul>\n<pre><code>t.string :address, :null =&gt; false\n<\/code><\/pre>\n<ul>\n<li>NOT NULL\uff0b\u30c7\u30d5\u30a9\u30eb\u30c8\u8a2d\u5b9a<\/li>\n<\/ul>\n<pre><code>t.string :address, :null =&gt; false, :default =&gt; 'Tokyo'\n\n<\/code><\/pre>\n<p>migrate\u30d5\u30a1\u30a4\u30eb\u306e\u4e00\u4f8b\uff1a<\/p>\n<pre><code>class CreateDevs &lt; ActiveRecord::Migration[5.2]\n  def change\n    create_table :devs do |t|\n      t.string :name , :default =&gt; 'NAMELESS'\n      t.string :userid , :null =&gt; false\n\n      t.timestamps\n    end\n    add_index  :devs, [:userid], unique: true\n  end\nend\n\n<\/code><\/pre>\n<p>\u3067\u304d\u305f\u3089\u3001<code>rake db:migrate<\/code>\u3002<\/p>\n<p>\u3067\u306f\u3001<code>default<\/code>\u3068\u304b<code>NOT NULL<\/code>\u3001\u30e6\u30cb\u30fc\u30af\u30ad\u30fc\u306e\u78ba\u8a8d\u3092\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n<pre><code># \u30c6\u30fc\u30d6\u30eb\u306e\u30ab\u30e9\u30e0\u306e\u78ba\u8a8d\nDev.column_names\n=&gt; [\"id\", \"name\", \"userid\", \"created_at\", \"updated_at\"]\n\n# \u6700\u521d\u306e\u30e6\u30fc\u30b6\u30fc\u4f5c\u6210\u3002\nirb(main):004:0&gt; Dev.create(name: 'yuis', userid: 'yuis')\n   (0.0ms)  begin transaction\n  Dev Create (1.0ms)  INSERT INTO \"devs\" (\"name\", \"userid\", \"created_at\", \"updated_at\") VALUES (?, ?, ?, ?)  [[\"name\", \"yuis\"], [\"userid\", \"yuis\"], [\"created_at\", \"2018-04-28 14:05:28.582549\"], [\"updated_at\", \"2018-04-28 14:05:28.582549\"]]\n   (61.7ms)  commit transaction\n+----+------+--------+-------------------------+-------------------------+\n| id | name | userid | created_at              | updated_at              |\n+----+------+--------+-------------------------+-------------------------+\n| 1  | yuis | yuis   | 2018-04-28 14:05:28 UTC | 2018-04-28 14:05:28 UTC |\n+----+------+--------+-------------------------+-------------------------+\n1 row in set\n\n\n# \u5168\u304f\u540c\u4e00\u306e\u30e6\u30fc\u30b6\u30fc\u540d\u3067\u3084\u3063\u3066\u307f\u308b\u3002\nirb(main):005:0&gt; Dev.create(name: 'yuis', userid: 'yuis')\n   (0.0ms)  begin transaction\n  Dev Create (0.0ms)  INSERT INTO \"devs\" (\"name\", \"userid\", \"created_at\", \"updated_at\") VALUES (?, ?, ?, ?)  [[\"name\", \"yuis\"], [\"userid\", \"yuis\"], [\"created_at\", \"2018-04-28 14:05:31.971999\"], [\"updated_at\", \"2018-04-28 14:05:31.971999\"]]\n   (0.0ms)  rollback transaction\nActiveRecord::RecordNotUnique: SQLite3::ConstraintException: UNIQUE constraint failed: devs.userid: INSERT INTO \"devs\" (\"name\", \"userid\", \"created_at\", \"updated_at\") VALUES (?, ?, ?, ?)\n        from (irb):5\n\n#=&gt; \u30e6\u30cb\u30fc\u30af\u30ad\u30fc\u5236\u7d04\u306b\u3088\u308b\u30a8\u30e9\u30fc\u3002\u3061\u3083\u3093\u3068\u6a5f\u80fd\u3057\u3066\u307e\u3059\u306d\u3002\n\n# userid\u3092\u5225\u306e\u5024\u306b\u3057\u3066\u518d\u691c\u8a3c\u3002\nirb(main):006:0&gt; Dev.create(name: 'yuis', userid: 'yuis_2')\n   (0.0ms)  begin transaction\n  Dev Create (1.0ms)  INSERT INTO \"devs\" (\"name\", \"userid\", \"created_at\", \"updated_at\") VALUES (?, ?, ?, ?)  [[\"name\", \"yuis\"], [\"userid\", \"yuis_2\"], [\"created_at\", \"2018-04-28 14:05:47.363096\"], [\"updated_at\", \"2018-04-28 14:05:47.363096\"]]\n   (60.3ms)  commit transaction\n+----+------+--------+-------------------------+-------------------------+\n| id | name | userid | created_at              | updated_at              |\n+----+------+--------+-------------------------+-------------------------+\n| 2  | yuis | yuis_2 | 2018-04-28 14:05:47 UTC | 2018-04-28 14:05:47 UTC |\n+----+------+--------+-------------------------+-------------------------+\n1 row in set\n\n# name\u306bNULL\u304c\u901a\u308b\u304b\u691c\u8a3c\u3002\nirb(main):007:0&gt; Dev.create(name: '', userid: 'yuis_3')\n   (0.0ms)  begin transaction\n  Dev Create (2.0ms)  INSERT INTO \"devs\" (\"name\", \"userid\", \"created_at\", \"updated_at\") VALUES (?, ?, ?, ?)  [[\"name\", \"\"], [\"userid\", \"yuis_3\"], [\"created_at\", \"2018-04-28 14:06:01.369127\"], [\"updated_at\", \"2018-04-28 14:06:01.369127\"]]\n   (58.2ms)  commit transaction\n+----+------+--------+-------------------------+-------------------------+\n| id | name | userid | created_at              | updated_at              |\n+----+------+--------+-------------------------+-------------------------+\n| 3  |      | yuis_3 | 2018-04-28 14:06:01 UTC | 2018-04-28 14:06:01 UTC |\n+----+------+--------+-------------------------+-------------------------+\n1 row in set\n\n# \u9593\u9055\u3048\u307e\u3057\u305f\u3002NULL\u3068\u3044\u3046\u5b9a\u6570\u306fRuby\u306b\u306f\u3042\u308a\u307e\u305b\u3093\u306d\u3002\nirb(main):008:0&gt; Dev.create(name: NULL, userid: 'yuis_4')\nNameError: uninitialized constant NULL\n        from (irb):8\n\n# nil\u306b\u3059\u308b\u3068default\u304c\u767a\u52d5\u3059\u308b\u306e\u304b\u691c\u8a3c\u3002\nirb(main):009:0&gt; Dev.create(name: nil, userid: 'yuis_4')\n   (0.0ms)  begin transaction\n  Dev Create (1.0ms)  INSERT INTO \"devs\" (\"name\", \"userid\", \"created_at\", \"updated_at\") VALUES (?, ?, ?, ?)  [[\"name\", nil], [\"userid\", \"yuis_4\"], [\"created_at\", \"2018-04-28 14:06:24.069276\"], [\"updated_at\", \"2018-04-28 14:06:24.069276\"]]\n   (75.2ms)  commit transaction\n+----+------+--------+-------------------------+-------------------------+\n| id | name | userid | created_at              | updated_at              |\n+----+------+--------+-------------------------+-------------------------+\n| 4  |      | yuis_4 | 2018-04-28 14:06:24 UTC | 2018-04-28 14:06:24 UTC |\n+----+------+--------+-------------------------+-------------------------+\n1 row in set\n\n#=&gt; \u767a\u52d5\u3057\u307e\u305b\u3093\u306d\u3002\u3084\u3063\u3071\u308aname\u306b\u3082`NOT NULL`\u5236\u7d04\u304c\u5fc5\u8981\u305d\u3046\u3067\u3059\u3002\n\n# name\u306b\u5024\u81ea\u4f53\u3092\u6307\u5b9a\u3057\u306a\u3044\u3002\nirb(main):010:0&gt; Dev.create(userid: 'yuis_5')\n   (0.0ms)  begin transaction\n  Dev Create (2.0ms)  INSERT INTO \"devs\" (\"userid\", \"created_at\", \"updated_at\") VALUES (?, ?, ?)  [[\"userid\", \"yuis_5\"], [\"created_at\", \"2018-04-28 14:06:41.119073\"], [\"updated_at\", \"2018-04-28 14:06:41.119073\"]]\n   (82.2ms)  commit transaction\n+----+----------+--------+-------------------------+-------------------------+\n| id | name     | userid | created_at              | updated_at              |\n+----+----------+--------+-------------------------+-------------------------+\n| 5  | NAMELESS | yuis_5 | 2018-04-28 14:06:41 UTC | 2018-04-28 14:06:41 UTC |\n+----+----------+--------+-------------------------+-------------------------+\n1 row in set\n\n#=&gt; \u3053\u308c\u306fdefault\u304c\u767a\u52d5\u3057\u307e\u3059\u3002\n\n<\/code><\/pre>\n<p>\u3053\u3093\u306a\u611f\u3058\u3067\u3001\u5236\u7d04\u306e\u304b\u3051\u65b9\u3068\u691c\u8a3c\u3067\u3057\u305f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Contents1 \u30d7\u30e9\u30a4\u30de\u30ea\u30fc\u30ad\u30fc\u3068\u30e6\u30cb\u30fc\u30af\u30ad\u30fc\u306e\u9055\u30441.1 \u4e00\u610f\u306e\u30e6\u30fc\u30b6\u30fcID\u3092\u5b9f\u88c5\u3059\u308b1.2 \u30e6\u30fc\u30b6\u30fcI [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[1],"tags":[],"acf":[],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8Rxwo-9S","_links":{"self":[{"href":"https:\/\/yuis-programming.com\/index.php?rest_route=\/wp\/v2\/posts\/612"}],"collection":[{"href":"https:\/\/yuis-programming.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yuis-programming.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yuis-programming.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yuis-programming.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=612"}],"version-history":[{"count":1,"href":"https:\/\/yuis-programming.com\/index.php?rest_route=\/wp\/v2\/posts\/612\/revisions"}],"predecessor-version":[{"id":613,"href":"https:\/\/yuis-programming.com\/index.php?rest_route=\/wp\/v2\/posts\/612\/revisions\/613"}],"wp:attachment":[{"href":"https:\/\/yuis-programming.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=612"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yuis-programming.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=612"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yuis-programming.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=612"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}