的SQLite3 :: ConstraintException:xxxxx.created_at不可以为null [英] SQLite3::ConstraintException: xxxxx.created_at may not be NULL

查看:181
本文介绍了的SQLite3 :: ConstraintException:xxxxx.created_at不可以为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个创建一个艺术家,并以用户为它关联一个resque工作。用户has_and_belongs_to_many:艺术家,艺术家has_and_belongs_to_many:用户

 高清self.perform(itunes_id,USER_ID =无)
  艺术家= Artist.find_by_itunes_id(itunes_id)|| lookup_and_cr​​eate_artist(itunes_id)
  如果USER_ID和放大器;&安培;用户= User.find(USER_ID)
    user.artists<<艺术家
    user.save!
  结束
结束
 

user.artists<<艺术家引发此异常:

 的ActiveRecord :: StatementInvalid:SQLite3的:: ConstraintException:artists_users.created_at不能为null:INSERT INTOartists_users(artist_id,USER_ID)
 

我也看到了建立一个artists_genres的(艺术家和流派有一个相互HABTM关系为好)

 的ActiveRecord :: StatementInvalid:SQLite3的:: ConstraintException:artists_genres.created_at不能为null:INSERT INTOartists_genres(artist_id,genre_id)
 

解决方案

原来,这是有目的的在轨,但在许多问题已经被提出了:

HABTM关系不会自动设置时间戳,所以我可以删除它们,或使用的has_many:艺术家:通过=> :artists_genres 相关

I have a resque job that creates an artist and associates it with a user. User has_and_belongs_to_many :artists, and Artist has_and_belongs_to_many :users.

def self.perform(itunes_id, user_id=nil)
  artist = Artist.find_by_itunes_id(itunes_id) || lookup_and_create_artist(itunes_id)
  if user_id && user = User.find(user_id)
    user.artists << artist
    user.save!
  end
end

user.artists << artist raises this exception:

ActiveRecord::StatementInvalid: SQLite3::ConstraintException: artists_users.created_at may not be NULL: INSERT INTO "artists_users" ("artist_id", "user_id")

I have also seen the creation of an artists_genres (Artist and Genre have a reciprocal HABTM relationship as well)

ActiveRecord::StatementInvalid: SQLite3::ConstraintException: artists_genres.created_at may not be NULL: INSERT INTO "artists_genres" ("artist_id", "genre_id")

解决方案

Turns out this is purposeful in Rails, but has been raised in numerous issues:

HABTM relationships don't automatically set timestamps so I can either remove them or use a has_many :artists, :through => :artists_genres relationship

这篇关于的SQLite3 :: ConstraintException:xxxxx.created_at不可以为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆