我有一个重新排序的工作,创建一个艺术家,并将其与用户关联。用户艺术家:artists和艺术家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
enduser.artists << artist会引发此异常:
ActiveRecord::StatementInvalid: SQLite3::ConstraintException: artists_users.created_at may not be NULL: INSERT INTO "artists_users" ("artist_id", "user_id")我也看到了artists_genres的创建(艺术家和流派也有一种互惠的关系)
ActiveRecord::StatementInvalid: SQLite3::ConstraintException: artists_genres.created_at may not be NULL: INSERT INTO "artists_genres" ("artist_id", "genre_id")发布于 2012-11-21 04:26:47
事实证明,这在Rails中是有目的的,但已经在许多问题中提出:
HABTM关系不会自动设置时间戳,因此我可以删除它们或使用has_many :artists, :through => :artists_genres关系
发布于 2012-11-20 07:45:07
听起来您的时间戳设置不正确。
如果您想通过创可贴来摆脱它,可以将这两个字段重新定义为NULLable (而不是NOT NULL)。
问题通常发生在应用程序配置错误时。
请确保
config.active_record.record_timestamps = true如果为false,则不会设置时间戳,您将看到约束错误。
发布于 2012-11-20 07:46:01
听起来您的迁移没有正确设置时间戳字段。它应该是这样的:
create_table :artists_users, :id => false do |t|
t.references :artist
t.references :user
t.timestamps
end
create_table :artists_genres, :id => false do |t|
t.references :artist
t.references :genre
t.timestamps
endhttps://stackoverflow.com/questions/13464299
复制相似问题