How to create first Files record whose parent references itself

I have a model:

class Files < ActiveRecord::Base
  belongs_to :parent, class_name: "Files"

As in a file system, I want the root entry to point to itself. I’ve tried various things but it always ends up with the parent field being null and the DB throws an error because the parent field can’t be null.

I could, I suppose, allow nulls and have the root entry have a null parent but I’d like to know how to do this anyway.

I don’t think you want a NOT NULL constraint. Instead I think you want a CHECK CONSTRAINT or RULE that ensures there’s only 1 with a NULL parent_id.

Having a self-referential root seems really weird, not to mention impossible to insert without inserting the record, then updating the record to have itself as an id and THEN adding the not null.