1

i have model question with text and token fields. Want to add data into this via scaffold.

This is my question_ controller

  def create
    # @question = Question.new(params[:question])
      @question = Question.create(:text => params[:text], :security_token => Digest::SHA1.hexdigest(rand(1000000).to_s))
    render :json => @question.to_ext_json(:success => @question.save)
  end

When i press "ADD" button i get in console this:

  Question Create (0.0ms)   Mysql::Error: Column 'text' cannot be null: INSERT INTO `questions` (`created_at`, `updated_at`, `text`, `security_token`) VALUES('2011-04-05 09:07:37', '2011-04-05 09:07:37', NULL, 'bf44551f11ce202b88d521a1826ab6db4254ce55')

Why COlumn 'text' can't be null?

1
  • It can't be null because table definition doesn't allow it to be null. Also, it's bad practice to use reserved words for column names in MySQL. Alter your table and remove "NOT NULL" part for that particular column definition. Commented Apr 5, 2011 at 9:15

5 Answers 5

1

You created the text column for the questions table with a NOT NULL constraint, and params[:text] is probably nil.

Since you used the scaffolding form params[:question][:text] returns the contents for text, not params[:text]!

Sign up to request clarification or add additional context in comments.

Comments

0

Because the column in the database table is defined as 'not null'?

Comments

0

You are passing an empty text value (NULL/nil) to a database field which has NOT NULL constraint defined. You need to either ensure, that text is never empty or release this constraint, allowing NULLable fiels in the MySQL database.

Comments

0

I would suggest that you add a validation in your model to verify if text is null. as such, you will be spared from this low level error.

Comments

0

This error is nothing to do with ruby or rails, its just because you have defined the column as not null (.. as everybody says... :D ) , You might want to check your migration to see if you defined the column as not null there..

cheers

sameera

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.