Note that there are some explanatory texts on larger screens.

plurals
  1. PO"DataObjects::IntegrityError (ERROR: null value in column violates not-null constraint" error in rails 3
    text
    copied!<p>In my model I have</p> <pre><code> class Alias include DataMapper::Resource belongs_to :user property :id, String, :key =&gt; true, :required =&gt; true, :unique =&gt; true validates_format_of :id, :with =&gt; /[0-9a-z\-]/i </code></pre> <p>end</p> <p>In my controller:</p> <pre><code>def new @new_alias = @owner.aliases.new() end def create @owner = current_user @alias = @owner.aliases.create(params[:alias]) end </code></pre> <p>And in my view</p> <pre><code>&lt;%= form_for @new_alias, :url =&gt; {:controller =&gt; "aliases", :action=&gt;"create"} do |f| %&gt; &lt;%= f.text_field :id, :placeholder =&gt; "Account name" %&gt;&lt;/br&gt; &lt;%= f.submit :value =&gt; "Create" %&gt; &lt;% end %&gt; </code></pre> <p>For me it looks preatty normal, but when I'm trying to save new alias, it results with with:</p> <pre><code>ERROR: null value in column "alias_id" violates not-null constraint </code></pre> <blockquote> <p>Processing by AliasesController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"/token=", "alias"=>{"id"=>"IDNAME"}, "commit"=>"Create"} ~ SQL (0.632ms) SELECT "id", "encrypted_password", "remember_created_at", "reset_password_token", "reset_password_sent_at", "failed_attempts", "unlock_token", "locked_at", "sign_in_count", "current_sign_in_at", "last_sign_in_at", "current_sign_in_ip", "last_sign_in_ip", "username", "email", "name", "country" FROM "users" WHERE "id" IN (2) LIMIT 1 ~<br> SQL (0.491ms) SELECT "id" FROM "aliases" WHERE "id" = 'IDNAME' ORDER BY "id" LIMIT 1 Completed in 11ms ~ SQL (0.531ms) INSERT INTO "aliases" ("id", "user_id") VALUES ('IDNAME', 2) ~ ERROR: null value in column "alias_id" violates not-null constraint (code: 33575106, sql state: 23502, query: INSERT INTO "aliases" ("id", "user_id") VALUES ('IDNAME', 2), uri: postgres:name@localhost:5432postgres?adapter=postgres&amp;host=localhost&amp;port=5432&amp;username=name&amp;password=pass&amp;database=postgres&amp;path=postgres&amp;schema_search_path=public&amp;encoding=utf8&amp;template=template0)</p> <p>DataObjects::IntegrityError (ERROR: null value in column "alias_id" violates not-null constraint ):<br> app/controllers/aliases_controller.rb:5:in `create'</p> </blockquote> <p>What could be the problem? I'm using rails3, postgres and datamapper. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload