在我的模型我有“数据对象:: IntegrityError(错误:在列空值违反非空约束” 在轨误差3
class Alias
include DataMapper::Resource
belongs_to :user
property :id, String, :key => true, :required => true, :unique => true
validates_format_of :id, :with => /[0-9a-z\-]/i
末
在我的控制器:
def new
@new_alias = @owner.aliases.new()
end
def create
@owner = current_user
@alias = @owner.aliases.create(params[:alias])
end
在我看来
<%= form_for @new_alias, :url => {:controller => "aliases", :action=>"create"} do |f| %>
<%= f.text_field :id, :placeholder => "Account name" %></br>
<%= f.submit :value => "Create" %>
<% end %>
对我来说,它看起来preatty正常,但是当我试图保存新的别名,它导致用:
ERROR: null value in column "alias_id" violates not-null constraint
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 ~
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:[email protected]:5432postgres?adapter=postgres&host=localhost&port=5432&username=name&password=pass&database=postgres&path=postgres&schema_search_path=public&encoding=utf8&template=template0)DataObjects::IntegrityError (ERROR: null value in column "alias_id" violates not-null constraint):
app/controllers/aliases_controller.rb:5:in `create'
可能是什么问题呢?我正在使用rails3,postgres和datamapper。