Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails rails 3中的“DataObjects::IntegrityError(错误:列中的null值违反非null约束”错误_Ruby On Rails_Postgresql_Datamapper - Fatal编程技术网

Ruby on rails rails 3中的“DataObjects::IntegrityError(错误:列中的null值违反非null约束”错误

Ruby on rails rails 3中的“DataObjects::IntegrityError(错误:列中的null值违反非null约束”错误,ruby-on-rails,postgresql,datamapper,Ruby On Rails,Postgresql,Datamapper,在我的模型中,我有 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

在我的模型中,我有

    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 %>
通过AliaseControllerCreate进行处理 作为HTML参数:{utf8=>✓, 真实性\u令牌=>/token=, 别名=>{id=>IDNAME}, commit=>Create}~SQL 0.632ms 选择id、加密密码、, 还记得你在, 重置密码\u令牌, 重置密码发送到, 失败的\u尝试,解锁\u令牌, 锁定,登录,计数, 当前登录地址:, 最后一次签名在, ip中的当前符号, 最后一次登录ip,用户名, 电子邮件、姓名、国家/地区 id在2中的用户限制为1~ SQL 0.491ms从中选择id id为'IDNAME'顺序的别名 按id限制1在11毫秒内完成~ SQL 0.531ms插入别名 id,用户id值'IDNAME',2 ~ERROR:列中的值为null 别名\u id不为空 约束代码:33575106,sql 状态:23502,查询:插入到 别名id、用户id值 'IDNAME',2,uri: 博士后:name@localhost:5432postgres?adapter=postgres&host=localhost&port=5432&username=name&password=pass&database=postgres&path=postgres&schema\u search\u path=public&encoding=utf8&template=template0

DataObjects::IntegrityError错误: 列别名\u id中的空值 违反非空约束: app/controllers/alias\u controller.rb:5:in `创造


有什么问题吗?我使用的是rails3、postgres和datamapper。

不熟悉rails的内部结构,但列的定义必须允许空值。

看来数据库中有一些字段没有在模型中定义,而且其中一些字段没有空约束。因为datamapper只写入它知道的字段。在本例中为id,它不会为表中可能存在的任何其他字段指定值。由于未指定的字段需要值,PgSQL将产生错误


要么删除NOTNULL约束,要么将这些字段添加到DataMapper中,并在其上添加:默认值。

我在data mapper中也遇到了同样的问题。 这就是我为修复它所做的:

在Alias类中,添加一行,如下所示:

property :user_id, Integer
此行定义外键

property :user_id, Integer