0

I command in Terminal to change on column in database to not null, but It seems not work.

rails g migration change_column_null :Speaker, :surname, false

I got a file ChangeColumnNull But inside, it is nothing.

class ChangeColumnNull < ActiveRecord::Migration
def change
end
end

Lecture Controller (Def Create):

class CplecturesController < ApplicationController
layout 'cp_layout'

 def create
@lecture = Lecture.new(lecture_params)

@lecture.save
redirect_to @lecture
end

private
def lecture_params
  params.require(:lecture).permit(:lecture_title, :lecture_day,   :column, :start_time, :end_time, :registered_speakers, :guest_speakers, :description)
end
end

Forms

 <%= form_for :lecture, url:lectures_path do |f| %>
 <form>
 <div class="form-group">
  <%=label_tag "Lecture Title" %><br>
  <%= f.text_field :lecture_title, :class => "form-control",   :placeholder => "Example: Why is Wordpress the best?" %>
</div>
Serjik
  • 10,543
  • 8
  • 61
  • 70
Joe
  • 107
  • 1
  • 10
  • This looks like a duplicate of: http://stackoverflow.com/questions/5966840/how-to-change-a-nullable-column-to-not-nullable-in-a-rails-migration – GoGoCarl Apr 25 '16 at 21:03

2 Answers2

6

Erase that migration and write a blank migration with a better name and then fill it out by setting a default value. If you have a default value it will never be null.

rails g migration ChangeColumnOnTableName

Then inside that migration do the following:

change_column :name_of_table, :name_of_column, :data_type_of_column, :null => false

If you're only worried about it being null based on what a user enters, you could simply add a validation that requires it. In your model:

validates :name_of_column, presence: true
toddmetheny
  • 4,405
  • 1
  • 22
  • 39
  • va185042:project Nixon$ rails g nameNull change_column :Speaker, :organisation, :string, :default => false va185042:project Nixon$ bundle exec rake db:migrate Nothing to annotate. After I command this, nothing happen, not even file... – Joe Apr 25 '16 at 21:05
  • +1 for the validation in the model, but why use a default value instead of `null: false`? Wouldn't you prefer to fail if data was input without a required field, rather than storing potentially garbage data with a guessed default? – GoGoCarl Apr 25 '16 at 21:05
  • @Joe that change_column goes inside the migration. Not as part of the migration command. You run them separately. I will update to show you. – toddmetheny Apr 25 '16 at 21:07
  • 1
    @GoGoCarl valid point. depends on why you don't want it to be null. We don't really have that information. If it's a boolean field, for instance, I think you might naturally want it to be true or false instead of enforced. But don't disagree in principle. That works, too. – toddmetheny Apr 25 '16 at 21:09
  • @toddmetheny va185042:project Nixon$ rails g migration change_column :Lecture, :lecture_title, :string, :default => false va185042:project Nixon$ bundle exec rake db:migrate Nothing to annotate. Am i right, still nothing happen......Sorry – Joe Apr 25 '16 at 21:11
  • @Joe I updated my answer do NOT put that after migration. that goes INSIDE the migration that is generated. – toddmetheny Apr 25 '16 at 21:12
  • @toddmetheny What did u mean inside MIGRATION?? – Joe Apr 25 '16 at 21:13
  • first run this: rails g migration ChangeColumnOnTableName – toddmetheny Apr 25 '16 at 21:14
  • Next open up the file that is generated – toddmetheny Apr 25 '16 at 21:14
  • Inside the change method add change_column with the information that you want – toddmetheny Apr 25 '16 at 21:15
  • after you run that first command you'll see something like this: `create db/migrate/20160425211553_blank_migration.rb` That's the file you need to open. The change_column command will go inside the `def change` that is inside of that file. – toddmetheny Apr 25 '16 at 21:16
  • YEAH, But the file I got is called 20160425211553_change_column_on_table_name.rb .... – Joe Apr 25 '16 at 21:19
  • @Joe. Open that file. Mine was just an example dude. You're taking everything too literally. – toddmetheny Apr 25 '16 at 21:20
  • Inside that file is where you need to add change_column with the name of your table, the name of your column, the datatype and :null => false – toddmetheny Apr 25 '16 at 21:21
  • Yeah, I think I got that......Thanks....But I just wonder why the original one in the RUBY ON RAILS GUIDE does not work ....... – Joe Apr 25 '16 at 21:21
  • Anywhere I say something like `table_name` it means for you to insert the name of your table name. `column_name` is the name of the column you want to change. Same with data type. Only :null => false is exact. – toddmetheny Apr 25 '16 at 21:23
  • class ChangeColumnOnLecture < ActiveRecord::Migration def change change_column :lectures, :lecture_title, :string, :null => false end end ChangeColumnOnLecture: migrating ============================ -- change_column(:lectures, :lecture_title, :string, {:null=>false}) -> 0.0031s == 20160425220032 ChangeColumnOnLecture: migrated (0.0032s) ================ BUT AFTER I HAVE DONE THIS, IT IS NOT WORKING ON MY RAILS APPLICATION, WHEN I SUBMIT WITHOUT TITLE, I CAN STILL SUBMIT..WHY @toddmetheny – Joe Apr 25 '16 at 22:04
  • in `lecture.rb` add `validates_presence_of :lecture_title` – toddmetheny Apr 25 '16 at 22:06
  • class Lecture < ActiveRecord::Base validates_presence_of :lecture_title end I CAN STILL SUBMIT EVEN WITHOUT LECTURE_TITLE @toddmetheny – Joe Apr 25 '16 at 22:09
  • Post the params hash that is coming through to the form. Are you sure the record is saving? Did you restart the server? You have to restart after a migration. – toddmetheny Apr 25 '16 at 22:10
  • Yeah I stop and start again. Yeah click submit, Nothing is saving...BUT NOT NULL should be tell me is can not submit and I should type something inside in lecture_title @toddmetheny – Joe Apr 25 '16 at 22:14
  • update your Question to include your form as well as the params hash being passed through the stack trace. – toddmetheny Apr 25 '16 at 22:15
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/110191/discussion-between-joe-and-toddmetheny). – Joe Apr 25 '16 at 22:20
  • Running to Doctor but will try to help later – toddmetheny Apr 25 '16 at 22:21
  • 1
    As a note, modern Rails recommends `validates :name_of_column, presence: true` instead of the legacy style. – tadman Apr 25 '16 at 22:23
1

if you are using latest ruby (2.5+) Here is the migration script to change fields from NOT NULL to NULL

class ChangeEmailPasswordToNullableUsers < ActiveRecord::Migration[5.2] def change change_column_null :users, :email, true change_column_null :users, :password, true end end

Manjunath Reddy
  • 1,039
  • 2
  • 13
  • 22