Hi –
On Tue, 16 Oct 2007, JoeRails wrote:
i get the following error:
Mysql::Error: Column ‘parent_id’ cannot be null: INSERT INTO
categories (name
, description
, parent_id
, created_at
)
VALUES(‘adfadfadfasdf’, ‘adsfasdfasdfasdf’, NULL, ‘2007-10-16
17:46:44’)
The error is that column ‘parent_id’ cannot be null
It’s strictly a database thing. Your database has a constraint that
this column can’t be null, so when you do:
@category = Category.create(params[:category]) # or whatever
it bombs if params[:category] is nil, because that gets inserted into
the database as NULL.
You have a couple of choices. If you want to (or need to) keep the
constraint, then you either need to test for nil and take corrective
action, or rescue from the Mysql::Error. Another approach is to allow
the NULL in the database, but put an application-level validation in
the model that ensures that every category has a parent.
How much data-checking to do at the database level, and how much at
the application level (i.e., before database assertion is even
attempted), is very dependent on lots of factors, some of them
organization (e.g., who has access to the database?). So you can’t
just make the decision for this one example; it needs to be thought
through carefully. But anyway, that’s the general area of the issue.
David
It’s something else. What error message are you getting?
–
Upcoming training from Ruby Power and Light, LLC:
- Intro to Ruby on Rails, Edison, NJ, October 23-26
- Advancing with Rails, Edison, NJ, November 6-9
Both taught by David A. Black.
See http://www.rubypal.com for more info!