title: SQLite Error 19: 'NOT NULL constraint failed: ef_temp_Posts.UserId
body: <p>I am making a simple blog app, the migration is generated successfully, but  when I write &quot;dotnet ef database update&quot;, I get an error &quot;SQLite Error 19: 'NOT NULL constraint failed: ef_temp_Posts.UserId'</p>
  <p>here is my migration:</p>
  <pre><code>     using System;
  using Microsoft.EntityFrameworkCore.Migrations;
   
  namespace API._Data.Migrations
  {
      public partial class addingIdentity3 : Migration
      {
          protected override void Up(MigrationBuilder migrationBuilder)
          {
              migrationBuilder.DropForeignKey(
                  name: &quot;FK_Posts_Users_UserId&quot;,
                  table: &quot;Posts&quot;);
   
              migrationBuilder.DropForeignKey(
                  name: &quot;FK_UsersPostComments_Users_UserId&quot;,
                  table: &quot;UsersPostComments&quot;);
   
              migrationBuilder.DropForeignKey(
                  name: &quot;FK_UsersPostLikes_Users_UserId&quot;,
                  table: &quot;UsersPostLikes&quot;);
   
              migrationBuilder.DropPrimaryKey(
                  name: &quot;PK_Users&quot;,
                  table: &quot;Users&quot;);
   
              migrationBuilder.DropColumn(
                  name: &quot;PasswordSalt&quot;,
                  table: &quot;Users&quot;);
   
              migrationBuilder.RenameTable(
                  name: &quot;Users&quot;,
                  newName: &quot;AspNetUsers&quot;);
   
              migrationBuilder.AlterColumn&lt;string&gt;(
                  name: &quot;PasswordHash&quot;,
                  table: &quot;AspNetUsers&quot;,
                  type: &quot;TEXT&quot;,
                  nullable: true,
                  oldClrType: typeof(byte[]),
                  oldType: &quot;BLOB&quot;,
                  oldNullable: true);
   
              migrationBuilder.AddColumn&lt;int&gt;(
                  name: &quot;AccessFailedCount&quot;,
                  table: &quot;AspNetUsers&quot;,
                  type: &quot;INTEGER&quot;,
                  nullable: false,
                  defaultValue: 0);
   
              migrationBuilder.AddColumn&lt;string&gt;(
                  name: &quot;ConcurrencyStamp&quot;,
                  table: &quot;AspNetUsers&quot;,
                  type: &quot;TEXT&quot;,
                  nullable: true);
   
              migrationBuilder.AddColumn&lt;bool&gt;(
                  name: &quot;EmailConfirmed&quot;,
                  table: &quot;AspNetUsers&quot;,
                  type: &quot;INTEGER&quot;,
                  nullable: false,
                  defaultValue: false);
   
              migrationBuilder.AddColumn&lt;bool&gt;(
                  name: &quot;LockoutEnabled&quot;,
                  table: &quot;AspNetUsers&quot;,
                  type: &quot;INTEGER&quot;,
                  nullable: false,
                  defaultValue: false);
   
              migrationBuilder.AddColumn&lt;DateTimeOffset&gt;(
                  name: &quot;LockoutEnd&quot;,
                  table: &quot;AspNetUsers&quot;,
                  type: &quot;TEXT&quot;,
                  nullable: true);
   
              migrationBuilder.AddColumn&lt;string&gt;(
                  name: &quot;NormalizedEmail&quot;,
                  table: &quot;AspNetUsers&quot;,
                  type: &quot;TEXT&quot;,
                  maxLength: 256,
                  nullable: true);
   
              migrationBuilder.AddColumn&lt;string&gt;(
                  name: &quot;NormalizedUserName&quot;,
                  table: &quot;AspNetUsers&quot;,
                  type: &quot;TEXT&quot;,
                  maxLength: 256,
                  nullable: true);
   
              migrationBuilder.AddColumn&lt;string&gt;(
                  name: &quot;PhoneNumber&quot;,
                  table: &quot;AspNetUsers&quot;,
                  type: &quot;TEXT&quot;,
                  nullable: true);
   
              migrationBuilder.AddColumn&lt;bool&gt;(
                  name: &quot;PhoneNumberConfirmed&quot;,
                  table: &quot;AspNetUsers&quot;,
                  type: &quot;INTEGER&quot;,
                  nullable: false,
                  defaultValue: false);
   
              migrationBuilder.AddColumn&lt;string&gt;(
                  name: &quot;SecurityStamp&quot;,
                  table: &quot;AspNetUsers&quot;,
                  type: &quot;TEXT&quot;,
                  nullable: true);
   
              migrationBuilder.AddColumn&lt;bool&gt;(
                  name: &quot;TwoFactorEnabled&quot;,
                  table: &quot;AspNetUsers&quot;,
                  type: &quot;INTEGER&quot;,
                  nullable: false,
                  defaultValue: false);
   
              migrationBuilder.AddPrimaryKey(
                  name: &quot;PK_AspNetUsers&quot;,
                  table: &quot;AspNetUsers&quot;,
                  column: &quot;Id&quot;);
   
              migrationBuilder.CreateTable(
                  name: &quot;AspNetRoles&quot;,
                  columns: table =&gt; new
                  {
                      Id = table.Column&lt;int&gt;(type: &quot;INTEGER&quot;, nullable: false)
                          .Annotation(&quot;Sqlite:Autoincrement&quot;, true),
                      Name = table.Column&lt;string&gt;(type: &quot;TEXT&quot;, maxLength: 256, nullable: true),
                      NormalizedName = table.Column&lt;string&gt;(type: &quot;TEXT&quot;, maxLength: 256, nullable: true),
                      ConcurrencyStamp = table.Column&lt;string&gt;(type: &quot;TEXT&quot;, nullable: true)
                  },
                  constraints: table =&gt;
                  {
                      table.PrimaryKey(&quot;PK_AspNetRoles&quot;, x =&gt; x.Id);
                  });
   
              migrationBuilder.CreateTable(
                  name: &quot;AspNetUserClaims&quot;,
                  columns: table =&gt; new
                  {
                      Id = table.Column&lt;int&gt;(type: &quot;INTEGER&quot;, nullable: false)
                          .Annotation(&quot;Sqlite:Autoincrement&quot;, true),
                      UserId = table.Column&lt;int&gt;(type: &quot;INTEGER&quot;, nullable: false),
                      ClaimType = table.Column&lt;string&gt;(type: &quot;TEXT&quot;, nullable: true),
                      ClaimValue = table.Column&lt;string&gt;(type: &quot;TEXT&quot;, nullable: true)
                  },
                  constraints: table =&gt;
                  {
                      table.PrimaryKey(&quot;PK_AspNetUserClaims&quot;, x =&gt; x.Id);
                      table.ForeignKey(
                          name: &quot;FK_AspNetUserClaims_AspNetUsers_UserId&quot;,
                          column: x =&gt; x.UserId,
                          principalTable: &quot;AspNetUsers&quot;,
                          principalColumn: &quot;Id&quot;,
                          onDelete: ReferentialAction.Cascade);
                  });
   
              migrationBuilder.CreateTable(
                  name: &quot;AspNetUserLogins&quot;,
                  columns: table =&gt; new
                  {
                      LoginProvider = table.Column&lt;string&gt;(type: &quot;TEXT&quot;, nullable: false),
                      ProviderKey = table.Column&lt;string&gt;(type: &quot;TEXT&quot;, nullable: false),
                      ProviderDisplayName = table.Column&lt;string&gt;(type: &quot;TEXT&quot;, nullable: true),
                      UserId = table.Column&lt;int&gt;(type: &quot;INTEGER&quot;, nullable: false)
                  },
                  constraints: table =&gt;
                  {
                      table.PrimaryKey(&quot;PK_AspNetUserLogins&quot;, x =&gt; new { x.LoginProvider, x.ProviderKey });
                      table.ForeignKey(
                          name: &quot;FK_AspNetUserLogins_AspNetUsers_UserId&quot;,
                          column: x =&gt; x.UserId,
                          principalTable: &quot;AspNetUsers&quot;,
                          principalColumn: &quot;Id&quot;,
                          onDelete: ReferentialAction.Cascade);
                  });
   
              migrationBuilder.CreateTable(
                  name: &quot;AspNetUserTokens&quot;,
                  columns: table =&gt; new
                  {
                      UserId = table.Column&lt;int&gt;(type: &quot;INTEGER&quot;, nullable: false),
                      LoginProvider = table.Column&lt;string&gt;(type: &quot;TEXT&quot;, nullable: false),
                      Name = table.Column&lt;string&gt;(type: &quot;TEXT&quot;, nullable: false),
                      Value = table.Column&lt;string&gt;(type: &quot;TEXT&quot;, nullable: true)
                  },
                  constraints: table =&gt;
                  {
                      table.PrimaryKey(&quot;PK_AspNetUserTokens&quot;, x =&gt; new { x.UserId, x.LoginProvider, x.Name });
                      table.ForeignKey(
                          name: &quot;FK_AspNetUserTokens_AspNetUsers_UserId&quot;,
                          column: x =&gt; x.UserId,
                          principalTable: &quot;AspNetUsers&quot;,
                          principalColumn: &quot;Id&quot;,
                          onDelete: ReferentialAction.Cascade);
                  });
   
              migrationBuilder.CreateTable(
                  name: &quot;AspNetRoleClaims&quot;,
                  columns: table =&gt; new
                  {
                      Id = table.Column&lt;int&gt;(type: &quot;INTEGER&quot;, nullable: false)
                          .Annotation(&quot;Sqlite:Autoincrement&quot;, true),
                      RoleId = table.Column&lt;int&gt;(type: &quot;INTEGER&quot;, nullable: false),
                      ClaimType = table.Column&lt;string&gt;(type: &quot;TEXT&quot;, nullable: true),
                      ClaimValue = table.Column&lt;string&gt;(type: &quot;TEXT&quot;, nullable: true)
                  },
                  constraints: table =&gt;
                  {
                      table.PrimaryKey(&quot;PK_AspNetRoleClaims&quot;, x =&gt; x.Id);
                      table.ForeignKey(
                          name: &quot;FK_AspNetRoleClaims_AspNetRoles_RoleId&quot;,
                          column: x =&gt; x.RoleId,
                          principalTable: &quot;AspNetRoles&quot;,
                          principalColumn: &quot;Id&quot;,
                          onDelete: ReferentialAction.Cascade);
                  });
   
              migrationBuilder.CreateTable(
                  name: &quot;AspNetUserRoles&quot;,
                  columns: table =&gt; new
                  {
                      UserId = table.Column&lt;int&gt;(type: &quot;INTEGER&quot;, nullable: false),
                      RoleId = table.Column&lt;int&gt;(type: &quot;INTEGER&quot;, nullable: false)
                  },
                  constraints: table =&gt;
                  {
                      table.PrimaryKey(&quot;PK_AspNetUserRoles&quot;, x =&gt; new { x.UserId, x.RoleId });
                      table.ForeignKey(
                          name: &quot;FK_AspNetUserRoles_AspNetRoles_RoleId&quot;,
                          column: x =&gt; x.RoleId,
                          principalTable: &quot;AspNetRoles&quot;,
                          principalColumn: &quot;Id&quot;,
                          onDelete: ReferentialAction.Cascade);
                      table.ForeignKey(
                          name: &quot;FK_AspNetUserRoles_AspNetUsers_UserId&quot;,
                          column: x =&gt; x.UserId,
                          principalTable: &quot;AspNetUsers&quot;,
                          principalColumn: &quot;Id&quot;,
                          onDelete: ReferentialAction.Cascade);
                  });
   
              migrationBuilder.CreateIndex(
                  name: &quot;EmailIndex&quot;,
                  table: &quot;AspNetUsers&quot;,
                  column: &quot;NormalizedEmail&quot;);
   
              migrationBuilder.CreateIndex(
                  name: &quot;UserNameIndex&quot;,
                  table: &quot;AspNetUsers&quot;,
                  column: &quot;NormalizedUserName&quot;,
                  unique: true);
   
              migrationBuilder.CreateIndex(
                  name: &quot;IX_AspNetRoleClaims_RoleId&quot;,
                  table: &quot;AspNetRoleClaims&quot;,
                  column: &quot;RoleId&quot;);
   
              migrationBuilder.CreateIndex(
                  name: &quot;RoleNameIndex&quot;,
                  table: &quot;AspNetRoles&quot;,
                  column: &quot;NormalizedName&quot;,
                  unique: true);
   
              migrationBuilder.CreateIndex(
                  name: &quot;IX_AspNetUserClaims_UserId&quot;,
                  table: &quot;AspNetUserClaims&quot;,
                  column: &quot;UserId&quot;);
   
              migrationBuilder.CreateIndex(
                  name: &quot;IX_AspNetUserLogins_UserId&quot;,
                  table: &quot;AspNetUserLogins&quot;,
                  column: &quot;UserId&quot;);
   
              migrationBuilder.CreateIndex(
                  name: &quot;IX_AspNetUserRoles_RoleId&quot;,
                  table: &quot;AspNetUserRoles&quot;,
                  column: &quot;RoleId&quot;);
   
              migrationBuilder.AddForeignKey(
                  name: &quot;FK_Posts_AspNetUsers_UserId&quot;,
                  table: &quot;Posts&quot;,
                  column: &quot;UserId&quot;,
                  principalTable: &quot;AspNetUsers&quot;,
                  principalColumn: &quot;Id&quot;,
                  onDelete: ReferentialAction.Cascade);
   
              migrationBuilder.AddForeignKey(
                  name: &quot;FK_UsersPostComments_AspNetUsers_UserId&quot;,
                  table: &quot;UsersPostComments&quot;,
                  column: &quot;UserId&quot;,
                  principalTable: &quot;AspNetUsers&quot;,
                  principalColumn: &quot;Id&quot;,
                  onDelete: ReferentialAction.Cascade);
   
              migrationBuilder.AddForeignKey(
                  name: &quot;FK_UsersPostLikes_AspNetUsers_UserId&quot;,
                  table: &quot;UsersPostLikes&quot;,
                  column: &quot;UserId&quot;,
                  principalTable: &quot;AspNetUsers&quot;,
                  principalColumn: &quot;Id&quot;,
                  onDelete: ReferentialAction.Cascade);
          }
   
          protected override void Down(MigrationBuilder migrationBuilder)
          {
              migrationBuilder.DropForeignKey(
                  name: &quot;FK_Posts_AspNetUsers_UserId&quot;,
                  table: &quot;Posts&quot;);
   
              migrationBuilder.DropForeignKey(
                  name: &quot;FK_UsersPostComments_AspNetUsers_UserId&quot;,
                  table: &quot;UsersPostComments&quot;);
   
              migrationBuilder.DropForeignKey(
                  name: &quot;FK_UsersPostLikes_AspNetUsers_UserId&quot;,
                  table: &quot;UsersPostLikes&quot;);
   
              migrationBuilder.DropTable(
                  name: &quot;AspNetRoleClaims&quot;);
   
              migrationBuilder.DropTable(
                  name: &quot;AspNetUserClaims&quot;);
   
              migrationBuilder.DropTable(
                  name: &quot;AspNetUserLogins&quot;);
   
              migrationBuilder.DropTable(
                  name: &quot;AspNetUserRoles&quot;);
   
              migrationBuilder.DropTable(
                  name: &quot;AspNetUserTokens&quot;);
   
              migrationBuilder.DropTable(
                  name: &quot;AspNetRoles&quot;);
   
              migrationBuilder.DropPrimaryKey(
                  name: &quot;PK_AspNetUsers&quot;,
                  table: &quot;AspNetUsers&quot;);
   
              migrationBuilder.DropIndex(
                  name: &quot;EmailIndex&quot;,
                  table: &quot;AspNetUsers&quot;);
   
              migrationBuilder.DropIndex(
                  name: &quot;UserNameIndex&quot;,
                  table: &quot;AspNetUsers&quot;);
   
              migrationBuilder.DropColumn(
                  name: &quot;AccessFailedCount&quot;,
                  table: &quot;AspNetUsers&quot;);
   
              migrationBuilder.DropColumn(
                  name: &quot;ConcurrencyStamp&quot;,
                  table: &quot;AspNetUsers&quot;);
   
              migrationBuilder.DropColumn(
                  name: &quot;EmailConfirmed&quot;,
                  table: &quot;AspNetUsers&quot;);
   
              migrationBuilder.DropColumn(
                  name: &quot;LockoutEnabled&quot;,
                  table: &quot;AspNetUsers&quot;);
   
              migrationBuilder.DropColumn(
                  name: &quot;LockoutEnd&quot;,
                  table: &quot;AspNetUsers&quot;);
   
              migrationBuilder.DropColumn(
                  name: &quot;NormalizedEmail&quot;,
                  table: &quot;AspNetUsers&quot;);
   
              migrationBuilder.DropColumn(
                  name: &quot;NormalizedUserName&quot;,
                  table: &quot;AspNetUsers&quot;);
   
              migrationBuilder.DropColumn(
                  name: &quot;PhoneNumber&quot;,
                  table: &quot;AspNetUsers&quot;);
   
              migrationBuilder.DropColumn(
                  name: &quot;PhoneNumberConfirmed&quot;,
                  table: &quot;AspNetUsers&quot;);
   
              migrationBuilder.DropColumn(
                  name: &quot;SecurityStamp&quot;,
                  table: &quot;AspNetUsers&quot;);
   
              migrationBuilder.DropColumn(
                  name: &quot;TwoFactorEnabled&quot;,
                  table: &quot;AspNetUsers&quot;);
   
              migrationBuilder.RenameTable(
                  name: &quot;AspNetUsers&quot;,
                  newName: &quot;Users&quot;);
   
              migrationBuilder.AlterColumn&lt;byte[]&gt;(
                  name: &quot;PasswordHash&quot;,
                  table: &quot;Users&quot;,
                  type: &quot;BLOB&quot;,
                  nullable: true,
                  oldClrType: typeof(string),
                  oldType: &quot;TEXT&quot;,
                  oldNullable: true);
   
              migrationBuilder.AddColumn&lt;byte[]&gt;(
                  name: &quot;PasswordSalt&quot;,
                  table: &quot;Users&quot;,
                  type: &quot;BLOB&quot;,
                  nullable: true);
   
              migrationBuilder.AddPrimaryKey(
                  name: &quot;PK_Users&quot;,
                  table: &quot;Users&quot;,
                  column: &quot;Id&quot;);
   
              migrationBuilder.AddForeignKey(
                  name: &quot;FK_Posts_Users_UserId&quot;,
                  table: &quot;Posts&quot;,
                  column: &quot;UserId&quot;,
                  principalTable: &quot;Users&quot;,
                  principalColumn: &quot;Id&quot;,
                  onDelete: ReferentialAction.Cascade);
   
              migrationBuilder.AddForeignKey(
                  name: &quot;FK_UsersPostComments_Users_UserId&quot;,
                  table: &quot;UsersPostComments&quot;,
                  column: &quot;UserId&quot;,
                  principalTable: &quot;Users&quot;,
                  principalColumn: &quot;Id&quot;,
                  onDelete: ReferentialAction.Cascade);
   
              migrationBuilder.AddForeignKey(
                  name: &quot;FK_UsersPostLikes_Users_UserId&quot;,
                  table: &quot;UsersPostLikes&quot;,
                  column: &quot;UserId&quot;,
                  principalTable: &quot;Users&quot;,
                  principalColumn: &quot;Id&quot;,
                  onDelete: ReferentialAction.Cascade);
          }
      }
  }
  </code></pre>
  <p>Everything works fine when I remove these from the migrations :</p>
  <pre><code>migrationBuilder.DropForeignKey(
                  name: &quot;FK_Posts_Users_UserId&quot;,
                  table: &quot;Posts&quot;);
   
  
  
               migrationBuilder.AddForeignKey(
                  name: &quot;FK_Posts_AspNetUsers_UserId&quot;,
                  table: &quot;Posts&quot;,
                  column: &quot;UserId&quot;,
                  principalTable: &quot;AspNetUsers&quot;,
                  principalColumn: &quot;Id&quot;,
                  onDelete: ReferentialAction.Cascade);
   
          protected override void Down(MigrationBuilder migrationBuilder)
          {
              migrationBuilder.DropForeignKey(
                  name: &quot;FK_Posts_AspNetUsers_UserId&quot;,
                  table: &quot;Posts&quot;);
   
              migrationBuilder.AddForeignKey(
                  name: &quot;FK_Posts_Users_UserId&quot;,
                  table: &quot;Posts&quot;,
                  column: &quot;UserId&quot;,
                  principalTable: &quot;Users&quot;,
                  principalColumn: &quot;Id&quot;,
                  onDelete: ReferentialAction.Cascade);
  }
  </code></pre>
  <p>what is the problem here, and how to fix it, please</p>