diff --git a/app/models/account.rb b/app/models/account.rb
index 686e740440761fc58ad6f70d1814b95c87d75ac7..5900fee4a326a4f82925fe08a722e2c8724b1f82 100644
--- a/app/models/account.rb
+++ b/app/models/account.rb
@@ -102,7 +102,7 @@ class Account < ApplicationRecord
   has_many :lists, through: :list_accounts
 
   # Account migrations
-  belongs_to :moved_to_account, class_name: 'Account'
+  belongs_to :moved_to_account, class_name: 'Account', optional: true
 
   scope :remote, -> { where.not(domain: nil) }
   scope :local, -> { where(domain: nil) }
diff --git a/app/models/account_domain_block.rb b/app/models/account_domain_block.rb
index 35810b6c2a2e8f251e80947329aba9581122df59..abcc923b315bf3db107fd475ecad9dce0c478fd6 100644
--- a/app/models/account_domain_block.rb
+++ b/app/models/account_domain_block.rb
@@ -13,7 +13,7 @@
 class AccountDomainBlock < ApplicationRecord
   include Paginable
 
-  belongs_to :account, required: true
+  belongs_to :account
   validates :domain, presence: true, uniqueness: { scope: :account_id }
 
   after_create  :remove_blocking_cache
diff --git a/app/models/admin/action_log.rb b/app/models/admin/action_log.rb
index 4e950fbf791b558451875baffd4a60dbbb1fdc62..c437c8ee8726fb5021c87bf49257b0830e935157 100644
--- a/app/models/admin/action_log.rb
+++ b/app/models/admin/action_log.rb
@@ -16,8 +16,8 @@
 class Admin::ActionLog < ApplicationRecord
   serialize :recorded_changes
 
-  belongs_to :account, required: true
-  belongs_to :target, required: true, polymorphic: true
+  belongs_to :account
+  belongs_to :target, polymorphic: true
 
   default_scope -> { order('id desc') }
 
diff --git a/app/models/block.rb b/app/models/block.rb
index 284abfe4c98d2eeb2c772bb674efeb441e58c40a..441e6bca308736268f590f19386e6e2ae2c1f0cd 100644
--- a/app/models/block.rb
+++ b/app/models/block.rb
@@ -13,8 +13,8 @@
 class Block < ApplicationRecord
   include Paginable
 
-  belongs_to :account, required: true
-  belongs_to :target_account, class_name: 'Account', required: true
+  belongs_to :account
+  belongs_to :target_account, class_name: 'Account'
 
   validates :account_id, uniqueness: { scope: :target_account_id }
 
diff --git a/app/models/conversation_mute.rb b/app/models/conversation_mute.rb
index 248cdfe6e4192722d5e271995e375e73388289ec..272eb81afd72f9385fa3901e3ae183d38665b6a0 100644
--- a/app/models/conversation_mute.rb
+++ b/app/models/conversation_mute.rb
@@ -9,6 +9,6 @@
 #
 
 class ConversationMute < ApplicationRecord
-  belongs_to :account, required: true
-  belongs_to :conversation, required: true
+  belongs_to :account
+  belongs_to :conversation
 end
diff --git a/app/models/favourite.rb b/app/models/favourite.rb
index c38838f2a84decf08942adcda75738659ea8591b..2b1271f31de8a2ca73974120812539840d08ee51 100644
--- a/app/models/favourite.rb
+++ b/app/models/favourite.rb
@@ -13,8 +13,8 @@
 class Favourite < ApplicationRecord
   include Paginable
 
-  belongs_to :account, inverse_of: :favourites, required: true
-  belongs_to :status,  inverse_of: :favourites, counter_cache: true, required: true
+  belongs_to :account, inverse_of: :favourites
+  belongs_to :status,  inverse_of: :favourites, counter_cache: true
 
   has_one :notification, as: :activity, dependent: :destroy
 
diff --git a/app/models/follow.rb b/app/models/follow.rb
index 3fb665afc74ccc2a1a4d613a792a6835af530401..f953b8e3e15d88bd84e7657a6660a598c6d331e8 100644
--- a/app/models/follow.rb
+++ b/app/models/follow.rb
@@ -14,12 +14,11 @@
 class Follow < ApplicationRecord
   include Paginable
 
-  belongs_to :account, counter_cache: :following_count, required: true
+  belongs_to :account, counter_cache: :following_count
 
   belongs_to :target_account,
              class_name: 'Account',
-             counter_cache: :followers_count,
-             required: true
+             counter_cache: :followers_count
 
   has_one :notification, as: :activity, dependent: :destroy
 
diff --git a/app/models/follow_request.rb b/app/models/follow_request.rb
index ebf6959cef77bb690fd83cb0981b6a97ce55f2b1..bd6c4a0b9ff7527c8472f58435ba23f517f3460c 100644
--- a/app/models/follow_request.rb
+++ b/app/models/follow_request.rb
@@ -14,8 +14,8 @@
 class FollowRequest < ApplicationRecord
   include Paginable
 
-  belongs_to :account, required: true
-  belongs_to :target_account, class_name: 'Account', required: true
+  belongs_to :account
+  belongs_to :target_account, class_name: 'Account'
 
   has_one :notification, as: :activity, dependent: :destroy
 
diff --git a/app/models/import.rb b/app/models/import.rb
index 091fb3044e61ba4bf83a579f383e77fa8f55747c..ba88435bfd0df57b58ab0e86314b834faabf071f 100644
--- a/app/models/import.rb
+++ b/app/models/import.rb
@@ -20,7 +20,7 @@ class Import < ApplicationRecord
 
   self.inheritance_column = false
 
-  belongs_to :account, required: true
+  belongs_to :account
 
   enum type: [:following, :blocking, :muting]
 
diff --git a/app/models/invite.rb b/app/models/invite.rb
index 6907c1f1d8f31fbd5841a446fa2b229748e2fee7..b87a3b7225009cf9ab46fbcbb6256666300a15ff 100644
--- a/app/models/invite.rb
+++ b/app/models/invite.rb
@@ -14,7 +14,7 @@
 #
 
 class Invite < ApplicationRecord
-  belongs_to :user, required: true
+  belongs_to :user
   has_many :users, inverse_of: :invite
 
   scope :available, -> { where(expires_at: nil).or(where('expires_at >= ?', Time.now.utc)) }
diff --git a/app/models/list.rb b/app/models/list.rb
index be85c3b878eaceba27da7906beca5b7253a56ea5..a2ec7e84a2764a2b77cef2fa740e2e5c1f886379 100644
--- a/app/models/list.rb
+++ b/app/models/list.rb
@@ -15,7 +15,7 @@ class List < ApplicationRecord
 
   PER_ACCOUNT_LIMIT = 50
 
-  belongs_to :account
+  belongs_to :account, optional: true
 
   has_many :list_accounts, inverse_of: :list, dependent: :destroy
   has_many :accounts, through: :list_accounts
diff --git a/app/models/list_account.rb b/app/models/list_account.rb
index 253932590358bf36d94390130e7c7b8ffafbdce3..da46cf032508baa3f7337adcd35d8696e6847464 100644
--- a/app/models/list_account.rb
+++ b/app/models/list_account.rb
@@ -10,9 +10,9 @@
 #
 
 class ListAccount < ApplicationRecord
-  belongs_to :list, required: true
-  belongs_to :account, required: true
-  belongs_to :follow, required: true
+  belongs_to :list
+  belongs_to :account
+  belongs_to :follow
 
   validates :account_id, uniqueness: { scope: :list_id }
 
diff --git a/app/models/media_attachment.rb b/app/models/media_attachment.rb
index abc5ab85421b98298bd265a348ed5a15e1aa303c..4b84b95fab110d4e060c023828588c0ed6483efb 100644
--- a/app/models/media_attachment.rb
+++ b/app/models/media_attachment.rb
@@ -45,8 +45,8 @@ class MediaAttachment < ApplicationRecord
     },
   }.freeze
 
-  belongs_to :account, inverse_of: :media_attachments
-  belongs_to :status,  inverse_of: :media_attachments
+  belongs_to :account, inverse_of: :media_attachments, optional: true
+  belongs_to :status,  inverse_of: :media_attachments, optional: true
 
   has_attached_file :file,
                     styles: ->(f) { file_styles f },
diff --git a/app/models/mention.rb b/app/models/mention.rb
index 14533e6a9e108b556bb4b48d1adf17a11140d7ee..f864bf8e1511a4fb2a5fd4b78e577fc97108e0d4 100644
--- a/app/models/mention.rb
+++ b/app/models/mention.rb
@@ -11,8 +11,8 @@
 #
 
 class Mention < ApplicationRecord
-  belongs_to :account, inverse_of: :mentions, required: true
-  belongs_to :status, required: true
+  belongs_to :account, inverse_of: :mentions
+  belongs_to :status
 
   has_one :notification, as: :activity, dependent: :destroy
 
diff --git a/app/models/mute.rb b/app/models/mute.rb
index 105696da63aecc49edbe07ac7ef67224425f50b3..948f22444ec836167e41f46cebc39cc023b593a6 100644
--- a/app/models/mute.rb
+++ b/app/models/mute.rb
@@ -14,8 +14,8 @@
 class Mute < ApplicationRecord
   include Paginable
 
-  belongs_to :account, required: true
-  belongs_to :target_account, class_name: 'Account', required: true
+  belongs_to :account
+  belongs_to :target_account, class_name: 'Account'
 
   validates :account_id, uniqueness: { scope: :target_account_id }
 
diff --git a/app/models/notification.rb b/app/models/notification.rb
index 97696352888fa9c04adfb6d3b98aac8c1ed04c1b..733f89cf794450a70f218f2da3fba2764ea7fc88 100644
--- a/app/models/notification.rb
+++ b/app/models/notification.rb
@@ -26,15 +26,15 @@ class Notification < ApplicationRecord
 
   STATUS_INCLUDES = [:account, :application, :stream_entry, :media_attachments, :tags, mentions: :account, reblog: [:stream_entry, :account, :application, :media_attachments, :tags, mentions: :account]].freeze
 
-  belongs_to :account
-  belongs_to :from_account, class_name: 'Account'
-  belongs_to :activity, polymorphic: true
-
-  belongs_to :mention,        foreign_type: 'Mention',       foreign_key: 'activity_id'
-  belongs_to :status,         foreign_type: 'Status',        foreign_key: 'activity_id'
-  belongs_to :follow,         foreign_type: 'Follow',        foreign_key: 'activity_id'
-  belongs_to :follow_request, foreign_type: 'FollowRequest', foreign_key: 'activity_id'
-  belongs_to :favourite,      foreign_type: 'Favourite',     foreign_key: 'activity_id'
+  belongs_to :account, optional: true
+  belongs_to :from_account, class_name: 'Account', optional: true
+  belongs_to :activity, polymorphic: true, optional: true
+
+  belongs_to :mention,        foreign_type: 'Mention',       foreign_key: 'activity_id', optional: true
+  belongs_to :status,         foreign_type: 'Status',        foreign_key: 'activity_id', optional: true
+  belongs_to :follow,         foreign_type: 'Follow',        foreign_key: 'activity_id', optional: true
+  belongs_to :follow_request, foreign_type: 'FollowRequest', foreign_key: 'activity_id', optional: true
+  belongs_to :favourite,      foreign_type: 'Favourite',     foreign_key: 'activity_id', optional: true
 
   validates :account_id, uniqueness: { scope: [:activity_type, :activity_id] }
   validates :activity_type, inclusion: { in: TYPE_CLASS_MAP.values }
diff --git a/app/models/report.rb b/app/models/report.rb
index c36f8db0a877b15595bfd8c541d64d58d8917af1..f55fb6d3eef22200fc69bbd370d709db4c723ad6 100644
--- a/app/models/report.rb
+++ b/app/models/report.rb
@@ -17,7 +17,7 @@
 class Report < ApplicationRecord
   belongs_to :account
   belongs_to :target_account, class_name: 'Account'
-  belongs_to :action_taken_by_account, class_name: 'Account'
+  belongs_to :action_taken_by_account, class_name: 'Account', optional: true
 
   scope :unresolved, -> { where(action_taken: false) }
   scope :resolved,   -> { where(action_taken: true) }
diff --git a/app/models/session_activation.rb b/app/models/session_activation.rb
index 1d4ebca021e4b1a3c47be50c4bfd69fa89155544..d364f03dfdded9e4fb630d65f78c828bff4084fa 100644
--- a/app/models/session_activation.rb
+++ b/app/models/session_activation.rb
@@ -15,9 +15,9 @@
 #
 
 class SessionActivation < ApplicationRecord
-  belongs_to :user, inverse_of: :session_activations, required: true
-  belongs_to :access_token, class_name: 'Doorkeeper::AccessToken', dependent: :destroy
-  belongs_to :web_push_subscription, class_name: 'Web::PushSubscription', dependent: :destroy
+  belongs_to :user, inverse_of: :session_activations
+  belongs_to :access_token, class_name: 'Doorkeeper::AccessToken', dependent: :destroy, optional: true
+  belongs_to :web_push_subscription, class_name: 'Web::PushSubscription', dependent: :destroy, optional: true
 
   delegate :token,
            to: :access_token,
diff --git a/app/models/status.rb b/app/models/status.rb
index 00dcec624d09173dcc82213de36bd6c1551e9316..26ff40bf7a00cce4b3a6b84c8a71f1ed8d598f97 100644
--- a/app/models/status.rb
+++ b/app/models/status.rb
@@ -33,14 +33,14 @@ class Status < ApplicationRecord
 
   enum visibility: [:public, :unlisted, :private, :direct], _suffix: :visibility
 
-  belongs_to :application, class_name: 'Doorkeeper::Application'
+  belongs_to :application, class_name: 'Doorkeeper::Application', optional: true
 
-  belongs_to :account, inverse_of: :statuses, counter_cache: true, required: true
-  belongs_to :in_reply_to_account, foreign_key: 'in_reply_to_account_id', class_name: 'Account'
-  belongs_to :conversation
+  belongs_to :account, inverse_of: :statuses, counter_cache: true
+  belongs_to :in_reply_to_account, foreign_key: 'in_reply_to_account_id', class_name: 'Account', optional: true
+  belongs_to :conversation, optional: true
 
-  belongs_to :thread, foreign_key: 'in_reply_to_id', class_name: 'Status', inverse_of: :replies
-  belongs_to :reblog, foreign_key: 'reblog_of_id', class_name: 'Status', inverse_of: :reblogs, counter_cache: :reblogs_count
+  belongs_to :thread, foreign_key: 'in_reply_to_id', class_name: 'Status', inverse_of: :replies, optional: true
+  belongs_to :reblog, foreign_key: 'reblog_of_id', class_name: 'Status', inverse_of: :reblogs, counter_cache: :reblogs_count, optional: true
 
   has_many :favourites, inverse_of: :status, dependent: :destroy
   has_many :reblogs, foreign_key: 'reblog_of_id', class_name: 'Status', inverse_of: :reblog, dependent: :destroy
diff --git a/app/models/status_pin.rb b/app/models/status_pin.rb
index a72c19750ebd4b79ef9827cba3798f1531411eca..d3a98d8bd301fe6c5763d4ed0aef329f3fa2ec02 100644
--- a/app/models/status_pin.rb
+++ b/app/models/status_pin.rb
@@ -11,8 +11,8 @@
 #
 
 class StatusPin < ApplicationRecord
-  belongs_to :account, required: true
-  belongs_to :status, required: true
+  belongs_to :account
+  belongs_to :status
 
   validates_with StatusPinValidator
 end
diff --git a/app/models/subscription.rb b/app/models/subscription.rb
index 7f2eeab91e5df906cf28878f9bd319a4b36208c5..ea11731607cf88d407e7628863235e496566b4b9 100644
--- a/app/models/subscription.rb
+++ b/app/models/subscription.rb
@@ -19,7 +19,7 @@ class Subscription < ApplicationRecord
   MIN_EXPIRATION = 1.day.to_i
   MAX_EXPIRATION = 30.days.to_i
 
-  belongs_to :account, required: true
+  belongs_to :account
 
   validates :callback_url, presence: true
   validates :callback_url, uniqueness: { scope: :account_id }
diff --git a/app/models/user.rb b/app/models/user.rb
index 8cad3221b47f2455086ed70f2cc5c7dc89969183..f6a533f84919d022e2a1462938ec2f39e70c4c3b 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -50,8 +50,8 @@ class User < ApplicationRecord
   devise :registerable, :recoverable, :rememberable, :trackable, :validatable,
          :confirmable
 
-  belongs_to :account, inverse_of: :user, required: true
-  belongs_to :invite, counter_cache: :uses
+  belongs_to :account, inverse_of: :user
+  belongs_to :invite, counter_cache: :uses, optional: true
   accepts_nested_attributes_for :account
 
   has_many :applications, class_name: 'Doorkeeper::Application', as: :owner
diff --git a/config/application.rb b/config/application.rb
index dc488ea8aaf398aa66ad8d3a69c5fa5b5be28367..453000abaeee6732b099f241293a1abdbff2e46e 100644
--- a/config/application.rb
+++ b/config/application.rb
@@ -18,6 +18,9 @@ require_relative '../lib/mastodon/redis_config'
 
 module Mastodon
   class Application < Rails::Application
+    # Initialize configuration defaults for originally generated Rails version.
+    config.load_defaults 5.1
+
     # Settings in config/environments/* take precedence over those specified here.
     # Application configuration should go into files in config/initializers
     # -- all .rb files in that directory are automatically loaded.