title: CoreData: How to achieve not null and unique constraint in one-to-one relationship?
body: <p>Currently, I have the following one-to-one relationship</p>
  <ol>
  <li>An <code>NSPlainNote</code> must have at least a <code>NSContent</code></li>
  <li><code>NSContent</code> will inversely point to a <code>NSPlainNote</code></li>
  </ol>
  <p>Here's how I define their relationship</p>
  <h2>NSPlainNote Entity</h2>
  <p><a href="https://i.stack.imgur.com/TQuiW.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TQuiW.png" alt="enter image description here" /></a></p>
  <hr />
  <h2>NSContent Entity</h2>
  <p><a href="https://i.stack.imgur.com/ZfeBx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ZfeBx.png" alt="enter image description here" /></a></p>
  <hr />
  <h2>Model class source code</h2>
  <pre><code>extension NSPlainNote: PlainNoteable {
      @NSManaged public var content: NSContent
  }
  
  extension NSPlainNote {
      convenience init(context: NSManagedObjectContext, plainNote: PlainNote) {
          self.init(context: context)
          
          self.copyFrom(context: context, plainNote: plainNote)
      }
  
      func copyFrom(context: NSManagedObjectContext, plainNote: PlainNote) {   
          if self.content.plainNote == nil {
              // Create new record
              self.content = NSContent(context: context)
              self.content.body = body
              self.content.plainNote = self
          } else {
              // Update existing record
              self.content.body = body
          }
      }
  }
  
  extension NSContent {
      @NSManaged public var body: String?
      @NSManaged public var plainNote: NSPlainNote?
  }
  </code></pre>
  <hr />
  <h2>Code to perform create/ update</h2>
  <pre><code>if let nsPlainNote = try backgroundContext.existingObject(with: nsManagedObjectID) as? NSPlainNote {
      // Update existing record
      nsPlainNote.copyFrom(
          context: backgroundContext,
          plainNote: plainNote
      )
  } else {
      // Create new record
      nsPlainNote = NSPlainNote(
          context: backgroundContext,
          plainNote: plainNote
      )
  }
  </code></pre>
  <hr />
  <p>What I wish to have is a safe mechanism to prevent me from</p>
  <ol>
  <li><code>NSContent</code> table contains a NULL value <code>NSPlainNote</code></li>
  <li><code>NSContent</code> table contains a duplicated value <code>NSPlainNote</code></li>
  </ol>
  <p>When I exam the generated SQLite code. It is</p>
  <pre><code>CREATE TABLE ZNSCONTENT ( 
      Z_PK INTEGER PRIMARY KEY, 
      Z_ENT INTEGER, 
      Z_OPT INTEGER, 
      ZPLAINNOTE INTEGER, 
      ZBODY VARCHAR 
  )
  
  CREATE INDEX ZNSCONTENT_ZPLAINNOTE_INDEX ON ZNSCONTENT (ZPLAINNOTE)
  </code></pre>
  <p>There is no mechanism to prevent NULL or duplication of <code>ZPLAINNOTE</code>.</p>
  <hr />
  <p>The reason I am having such concern is because during row updating, instead of executing correct logic</p>
  <pre><code>if self.content.plainNote == nil {
      // Create new record
      self.content = NSContent(context: context)
      self.content.body = body
      self.content.plainNote = self
  } else {
      // Update existing record
      self.content.body = body
  }
  </code></pre>
  <p>I accidentally execute</p>
  <pre><code>self.content = NSContent(context: context)
  self.content.body = body
  self.content.plainNote = self
  </code></pre>
  <p>This causes the <code>NSContent</code> old row's <code>ZPLAINNOTE</code> turned to NULL, and a new <code>NSContent</code> row is created.</p>
  <hr />
  <p>I would like to have <code>NOT NULL</code> and <code>UNIQUE</code> constraint at the <code>ZPLAINNOTE</code> column of <code>NSContent</code>. May I know, how I can achieve so via CoreData editor?</p>
  <p>Thanks.</p>