'Model Edit Migration Issues
I'm trying to edit a model to make the columns not null, not sure what I'm doing wrong.
I created a migration file, wrote my desired changes, and ran db:migrate.
Migration File
class ChangeFieldsToNotNull < ActiveRecord::Migration[7.0]
def change
change_column :events, :event_name, :string, null: false
change_column :events, :f1, :string, null: false
change_column :events, :f2, :string, null: false
end
end
Schema Result
ActiveRecord::Schema[7.0].define(version: 2022_04_20_233006) do
create_table "events", force: :cascade do |t|
t.string "event_name", null: false
t.string "f1", null: false
t.string "f2", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
After making these changes I tried to submit all null entries into the database, and it went straight in with no issues. Why is the DB accepting null entries when I just added null:false to all attributes?
#<Event:0x0000022826ff12e8 id: 2, event_name: "", f1: "", f2: "", created_at: Wed, 20 Apr 2022 23:39:10.196725000 UTC +00:00, updated_at: Wed, 20 Apr 2022 23:39:10.196725000 UTC +00:00>,
#<Event:0x0000022826ff1220 id: 3, event_name: "", f1: "", f2: "", created_at: Wed, 20 Apr 2022 23:44:53.247575000 UTC +00:00, updated_at: Wed, 20 Apr 2022 23:44:53.247575000 UTC +00:00>,
#<Event:0x0000022826ff1158 id: 4, event_name: "", f1: "", f2: "", created_at: Wed, 20 Apr 2022 23:50:50.998929000 UTC +00:00, updated_at: Wed, 20 Apr 2022 23:50:50.998929000 UTC +00:00>]
Edit***
Record Creation Method
class AddeventController < ApplicationController
def new
@event = Event.new
end
def create
@event = Event.new(event_params)
if @event.save
redirect_to root_path
else
render :new
end
end
private
def event_params
params.require(:event).permit(:event_name, :f1, :f2)
end
end
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
