Vitali Zakharoff
Vitali Zakharoff

Reputation: 353

Problem with MYSQL (INSERT)

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?

Upvotes: 1

Views: 242

Answers (5)

sameera207
sameera207

Reputation: 16629

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

Upvotes: 0

Neelesh
Neelesh

Reputation: 496

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.

Upvotes: 0

Malte
Malte

Reputation: 1200

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]!

Upvotes: 1

Sebastian Zaklada
Sebastian Zaklada

Reputation: 2376

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.

Upvotes: 0

honeyp0t
honeyp0t

Reputation: 897

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

Upvotes: 0

Related Questions