# Buggy code
def save_to_db(self):
    """Save to database."""
    if not self.dirty:
        return

    log.debug(
        "{id}: Saving to database: {show}", {"id": self.series_id, "show": self.name}
    )

    control_value_dict = {"indexer": self.indexer, "indexer_id": self.series_id}
    new_value_dict = {
        "show_name": self.name,
        "location": self.location,  # skip location validation
        "network": self.network,
        "genre": self.genre,
        "classification": self.classification,
        "runtime": self.runtime,
        "quality": self.quality,
        "airs": self.airs,
        "status": self.status,
        "flatten_folders": not self.season_folders,  # TODO: Remove negation after DB change
        "paused": self.paused,
        "air_by_date": self.air_by_date,
        "anime": self.anime,
        "scene": self.scene,
        "sports": self.sports,
        "subtitles": self.subtitles,
        "dvdorder": self.dvd_order,
        "startyear": self.start_year,
        "lang": self.lang,
        "imdb_id": self.imdb_id,
        "last_update_indexer": self.last_update_indexer,
        "rls_ignore_words": self.rls_ignore_words,
        "rls_require_words": self.rls_require_words,
        "default_ep_status": self.default_ep_status,
        "plot": self.plot,
        "airdate_offset": self.airdate_offset,
    }

    main_db_con = db.DBConnection()
    main_db_con.upsert("tv_shows", new_value_dict, control_value_dict)

    helpers.update_anime_support()

    if self.imdb_id and self.imdb_info.get("year"):
        control_value_dict = {"indexer": self.indexer, "indexer_id": self.series_id}
        new_value_dict = self.imdb_info

        main_db_con = db.DBConnection()
        main_db_con.upsert("imdb_info", new_value_dict, control_value_dict)

    self.reset_dirty()


# Traceback (AttributeError)
2019-02-22 03:02:57 DEBUG    Thread_6 :: [ee861d0] Found codec for Show Name Ep Name
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] Matched release Show.Name.003.HDTV.x264-RLSGROUP to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] Matched release Show.Name.003.HDTV.x264-RLSGROUP to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_1 :: [ee861d0] Matched release Show.Name.003.HDTV.x264-RLSGROUP to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] Matched release Show.Name.003.HDTV.x264-RLSGROUP to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_16 :: [ee861d0] Parsed Show.Name.003.HDTV.x264-RLSGROUP into title: Show Name, episode: [], absolute_episode: [3], source: HDTV, video_codec: H.264, video_encoder: x264, release_group: RLSGROUP, type: episode, parsing_time: 0.018264055252075195, season: None, quality: Unknown, total_time: 0.018332958221435547
2019-02-22 03:02:57 DEBUG    Thread_6 :: [ee861d0] 1: Episode has no release group, replacing it with RLSGROUP
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] Parsed Show.Name.003.HDTV.x264-RLSGROUP into title: Show Name, episode: [], absolute_episode: [3], source: HDTV, video_codec: H.264, video_encoder: x264, release_group: RLSGROUP, type: episode, parsing_time: 0.027241230010986328, season: None, quality: Unknown, total_time: 0.02730083465576172
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] Parsed Show.Name.003.HDTV.x264-RLSGROUP into title: Show Name, episode: [], absolute_episode: [3], source: HDTV, video_codec: H.264, video_encoder: x264, release_group: RLSGROUP, type: episode, parsing_time: 0.024758100509643555, season: None, quality: Unknown, total_time: 0.02481818199157715
2019-02-22 03:02:57 DEBUG    Thread_1 :: [ee861d0] Parsed Show.Name.003.HDTV.x264-RLSGROUP into title: Show Name, episode: [], absolute_episode: [3], source: HDTV, video_codec: H.264, video_encoder: x264, release_group: RLSGROUP, type: episode, parsing_time: 0.04372978210449219, season: None, quality: Unknown, total_time: 0.04378795623779297
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] Parsed Show.Name.003.HDTV.x264-RLSGROUP into title: Show Name, episode: [], absolute_episode: [3], source: HDTV, video_codec: H.264, video_encoder: x264, release_group: RLSGROUP, type: episode, parsing_time: 0.024518966674804688, season: None, quality: Unknown, total_time: 0.024566173553466797
2019-02-22 03:02:57 DEBUG    Thread_16 :: [ee861d0] Found codec for Show Name Ep Name
2019-02-22 03:02:57 ERROR    Thread_6 :: [ee861d0] Exception generated: 'TVShow' object has no attribute 'identifier'
Traceback (most recent call last):
  File "/Users/ovidijus/Apps/Medusa/medusa/server/web/core/base.py", line 262, in async_call
    result = function(**kwargs)
  File "/Users/ovidijus/Apps/Medusa/medusa/server/web/config/post_processing.py", line 130, in testNaming
    result = naming.test_name(pattern, multi, abd, sports, anime_type)
  File "/Users/ovidijus/Apps/Medusa/medusa/naming.py", line 298, in test_name
    return {'name': ep.formatted_filename(pattern, multi, anime_type), 'dir': ep.formatted_dir(pattern, multi)}
  File "/Users/ovidijus/Apps/Medusa/medusa/tv/episode.py", line 1830, in formatted_filename
    return sanitize_filename(self._format_pattern(name_groups[-1], multi, anime_type))
  File "/Users/ovidijus/Apps/Medusa/medusa/tv/episode.py", line 1682, in _format_pattern
    other_ep != self.related_episodes[-1]:
  File "/Users/ovidijus/Apps/Medusa/medusa/tv/episode.py", line 2066, in __eq__
    return all([self.series.identifier == other.series.identifier,
AttributeError: 'TVShow' object has no attribute 'identifier'
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] Found codec for Show Name Ep Name
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] Found codec for Show Name Ep Name
2019-02-22 03:02:57 DEBUG    Thread_1 :: [ee861d0] Found codec for Show Name Ep Name
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] Found codec for Show Name Ep Name
2019-02-22 03:02:57 DEBUG    Thread_16 :: [ee861d0] 1: Formatting pattern: Season %S -> Season 2
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] 1: Formatting pattern: Season %S -> Season 2
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] 1: Formatting pattern: Season %S -> Season 2
2019-02-22 03:02:57 DEBUG    Thread_1 :: [ee861d0] 1: Formatting pattern: Season %S -> Season 2
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] 1: Episode has no release group, replacing it with RLSGROUP
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] Trying to parse Season 2/Show Name S02E03.ext
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] Trying to parse Season 2/Show Name S02E03.ext
2019-02-22 03:02:57 DEBUG    Thread_1 :: [ee861d0] Trying to parse Season 2/Show Name S02E03.ext
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] 1: Formatting pattern: %SN S%0SE03 -> Show Name S02E03
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] Matched release Season 2/Show Name S02E03.ext to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] Matched release Season 2/Show Name S02E03.ext to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_1 :: [ee861d0] Matched release Season 2/Show Name S02E03.ext to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] Parsed Season 2/Show Name S02E03.ext into season: 2, title: Show Name, episode: [3], episode_title: ext, mimetype: application/vnd.novadigm.ext, type: episode, parsing_time: 0.02576303482055664, absolute_episode: [], quality: Unknown, total_time: 0.02585601806640625
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] Parsed Season 2/Show Name S02E03.ext into season: 2, title: Show Name, episode: [3], episode_title: ext, mimetype: application/vnd.novadigm.ext, type: episode, parsing_time: 0.036277055740356445, absolute_episode: [], quality: Unknown, total_time: 0.03634905815124512
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] Matched release Show.Name.003.HDTV.x264-RLSGROUP to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_1 :: [ee861d0] Parsed Season 2/Show Name S02E03.ext into season: 2, title: Show Name, episode: [3], episode_title: ext, mimetype: application/vnd.novadigm.ext, type: episode, parsing_time: 0.030246973037719727, absolute_episode: [], quality: Unknown, total_time: 0.030331134796142578
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] The name Season 2/Show Name S02E03.ext parsed into season: 2, title: Show Name, episode: [3], episode_title: ext, mimetype: application/vnd.novadigm.ext, type: episode, parsing_time: 0.02576303482055664, absolute_episode: [], quality: Unknown, total_time: 0.02585601806640625
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] The name Season 2/Show Name S02E03.ext parsed into season: 2, title: Show Name, episode: [3], episode_title: ext, mimetype: application/vnd.novadigm.ext, type: episode, parsing_time: 0.036277055740356445, absolute_episode: [], quality: Unknown, total_time: 0.03634905815124512
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] Parsed Show.Name.003.HDTV.x264-RLSGROUP into title: Show Name, episode: [], absolute_episode: [3], source: HDTV, video_codec: H.264, video_encoder: x264, release_group: RLSGROUP, type: episode, parsing_time: 0.016936779022216797, season: None, quality: Unknown, total_time: 0.01699090003967285
2019-02-22 03:02:57 DEBUG    Thread_1 :: [ee861d0] The name Season 2/Show Name S02E03.ext parsed into season: 2, title: Show Name, episode: [3], episode_title: ext, mimetype: application/vnd.novadigm.ext, type: episode, parsing_time: 0.030246973037719727, absolute_episode: [], quality: Unknown, total_time: 0.030331134796142578
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] Found codec for Show Name Ep Name
2019-02-22 03:02:57 DEBUG    Thread_1 :: [ee861d0] Checking whether the pattern Season %S/%SN S%0SE%0E is valid for a multi episode
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] Matched release Show.Name.003.HDTV.x264-RLSGROUP to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] Matched release Show.Name.003.HDTV.x264-RLSGROUP to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] 1: Formatting pattern: Season %S -> Season 2
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] Parsed Show.Name.003.HDTV.x264-RLSGROUP into title: Show Name, episode: [], absolute_episode: [3], source: HDTV, video_codec: H.264, video_encoder: x264, release_group: RLSGROUP, type: episode, parsing_time: 0.013588905334472656, season: None, quality: Unknown, total_time: 0.013649940490722656
2019-02-22 03:02:57 DEBUG    Thread_1 :: [ee861d0] Matched release Show.Name.003-004.HDTV.x264-RLSGROUP to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] Parsed Show.Name.003.HDTV.x264-RLSGROUP into title: Show Name, episode: [], absolute_episode: [3], source: HDTV, video_codec: H.264, video_encoder: x264, release_group: RLSGROUP, type: episode, parsing_time: 0.027174949645996094, season: None, quality: Unknown, total_time: 0.027235031127929688
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] Trying to parse Season 2/Show Name S02E03.ext
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] Found codec for Show Name Ep Name
2019-02-22 03:02:57 DEBUG    Thread_1 :: [ee861d0] Parsed Show.Name.003-004.HDTV.x264-RLSGROUP into title: Show Name, absolute_episode: [3, 4], episode: [], source: HDTV, video_codec: H.264, video_encoder: x264, release_group: RLSGROUP, type: episode, parsing_time: 0.016008853912353516, season: None, quality: Unknown, total_time: 0.016068220138549805
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] Found codec for Show Name Ep Name
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] Matched release Season 2/Show Name S02E03.ext to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] 1: Episode has no release group, replacing it with RLSGROUP
2019-02-22 03:02:57 DEBUG    Thread_1 :: [ee861d0] Found codec for Show Name Ep Name
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] 1: Episode has no release group, replacing it with RLSGROUP
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] Parsed Season 2/Show Name S02E03.ext into season: 2, title: Show Name, episode: [3], episode_title: ext, mimetype: application/vnd.novadigm.ext, type: episode, parsing_time: 0.015719890594482422, absolute_episode: [], quality: Unknown, total_time: 0.0158078670501709
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] 1: Formatting pattern: %SN S%0SE03 -> Show Name S02E03
2019-02-22 03:02:57 DEBUG    Thread_1 :: [ee861d0] 1: Episode has no release group, replacing it with RLSGROUP
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] 1: Formatting pattern: %SN S%0SE03 -> Show Name S02E03
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] The name Season 2/Show Name S02E03.ext parsed into season: 2, title: Show Name, episode: [3], episode_title: ext, mimetype: application/vnd.novadigm.ext, type: episode, parsing_time: 0.015719890594482422, absolute_episode: [], quality: Unknown, total_time: 0.0158078670501709
2019-02-22 03:02:57 ERROR    Thread_1 :: [ee861d0] Exception generated: 'TVShow' object has no attribute 'identifier'
Traceback (most recent call last):
  File "/Users/ovidijus/Apps/Medusa/medusa/server/web/core/base.py", line 262, in async_call
    result = function(**kwargs)
  File "/Users/ovidijus/Apps/Medusa/medusa/server/web/config/post_processing.py", line 160, in isNamingValid
    is_valid = naming.check_valid_naming(pattern, multi, anime_type)
  File "/Users/ovidijus/Apps/Medusa/medusa/naming.py", line 151, in check_valid_naming
    valid = valid and validate_name(pattern, multi, anime_type)
  File "/Users/ovidijus/Apps/Medusa/medusa/naming.py", line 201, in validate_name
    new_name = ep.formatted_filename(pattern, multi, anime_type) + '.ext'
  File "/Users/ovidijus/Apps/Medusa/medusa/tv/episode.py", line 1830, in formatted_filename
    return sanitize_filename(self._format_pattern(name_groups[-1], multi, anime_type))
  File "/Users/ovidijus/Apps/Medusa/medusa/tv/episode.py", line 1682, in _format_pattern
    other_ep != self.related_episodes[-1]:
  File "/Users/ovidijus/Apps/Medusa/medusa/tv/episode.py", line 2066, in __eq__
    return all([self.series.identifier == other.series.identifier,
AttributeError: 'TVShow' object has no attribute 'identifier'
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] Matched release Show.Name.003.HDTV.x264-RLSGROUP to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] Checking whether the pattern Season %S/%SN S%0SE%0E is valid for a multi episode
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] Matched release Show.Name.003.HDTV.x264-RLSGROUP to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] Parsed Show.Name.003.HDTV.x264-RLSGROUP into title: Show Name, episode: [], absolute_episode: [3], source: HDTV, video_codec: H.264, video_encoder: x264, release_group: RLSGROUP, type: episode, parsing_time: 0.014174938201904297, season: None, quality: Unknown, total_time: 0.014236927032470703
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] Matched release Show.Name.003-004.HDTV.x264-RLSGROUP to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] Parsed Show.Name.003.HDTV.x264-RLSGROUP into title: Show Name, episode: [], absolute_episode: [3], source: HDTV, video_codec: H.264, video_encoder: x264, release_group: RLSGROUP, type: episode, parsing_time: 0.013724088668823242, season: None, quality: Unknown, total_time: 0.013782978057861328
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] Found codec for Show Name Ep Name
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] Parsed Show.Name.003-004.HDTV.x264-RLSGROUP into title: Show Name, absolute_episode: [3, 4], episode: [], source: HDTV, video_codec: H.264, video_encoder: x264, release_group: RLSGROUP, type: episode, parsing_time: 0.015986919403076172, season: None, quality: Unknown, total_time: 0.016047000885009766
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] Found codec for Show Name Ep Name
2019-02-22 03:02:58 DEBUG    Thread_39 :: [ee861d0] 1: Formatting pattern: Season %S -> Season 2
2019-02-22 03:02:58 DEBUG    Thread_3 :: [ee861d0] Found codec for Show Name Ep Name
2019-02-22 03:02:58 DEBUG    Thread_0 :: [ee861d0] 1: Formatting pattern: Season %S -> Season 2
2019-02-22 03:02:58 DEBUG    Thread_39 :: [ee861d0] Trying to parse Show Name S02E03.ext
2019-02-22 03:02:58 DEBUG    Thread_3 :: [ee861d0] 1: Episode has no release group, replacing it with RLSGROUP
2019-02-22 03:02:58 DEBUG    Thread_0 :: [ee861d0] Trying to parse Show Name S02E03.ext
2019-02-22 03:02:58 ERROR    Thread_3 :: [ee861d0] Exception generated: 'TVShow' object has no attribute 'identifier'
Traceback (most recent call last):
  File "/Users/ovidijus/Apps/Medusa/medusa/server/web/core/base.py", line 262, in async_call
    result = function(**kwargs)
  File "/Users/ovidijus/Apps/Medusa/medusa/server/web/config/post_processing.py", line 160, in isNamingValid
    is_valid = naming.check_valid_naming(pattern, multi, anime_type)
  File "/Users/ovidijus/Apps/Medusa/medusa/naming.py", line 151, in check_valid_naming
    valid = valid and validate_name(pattern, multi, anime_type)
  File "/Users/ovidijus/Apps/Medusa/medusa/naming.py", line 201, in validate_name
    new_name = ep.formatted_filename(pattern, multi, anime_type) + '.ext'
  File "/Users/ovidijus/Apps/Medusa/medusa/tv/episode.py", line 1830, in formatted_filename
    return sanitize_filename(self._format_pattern(name_groups[-1], multi, anime_type))
  File "/Users/ovidijus/Apps/Medusa/medusa/tv/episode.py", line 1682, in _format_pattern
    other_ep != self.related_episodes[-1]:
  File "/Users/ovidijus/Apps/Medusa/medusa/tv/episode.py", line 2066, in __eq__
    return all([self.series.identifier == other.series.identifier,
AttributeError: 'TVShow' object has no attribute 'identifier'

# Fixed code
def save_to_db(self):
    """Save to database."""
    if not self.dirty:
        return

    log.debug(
        "{id}: Saving to database: {show}", {"id": self.series_id, "show": self.name}
    )

    control_value_dict = {"indexer": self.indexer, "indexer_id": self.series_id}
    new_value_dict = {
        "show_name": self.name,
        "location": self.location,  # skip location validation
        "network": self.network,
        "genre": self.genre,
        "classification": self.classification,
        "runtime": self.runtime,
        "quality": self.quality,
        "airs": self.airs,
        "status": self.status,
        "flatten_folders": not self.season_folders,  # TODO: Remove negation after DB change
        "paused": self.paused,
        "air_by_date": self.air_by_date,
        "anime": self.anime,
        "scene": self.scene,
        "sports": self.sports,
        "subtitles": self.subtitles,
        "dvdorder": self.dvd_order,
        "startyear": self.start_year,
        "lang": self.lang,
        "imdb_id": self.imdb_id,
        "last_update_indexer": self.last_update_indexer,
        "rls_ignore_words": self.rls_ignore_words,
        "rls_require_words": self.rls_require_words,
        "rls_ignore_exclude": self.rls_ignore_exclude,
        "rls_require_exclude": self.rls_require_exclude,
        "default_ep_status": self.default_ep_status,
        "plot": self.plot,
        "airdate_offset": self.airdate_offset,
    }

    main_db_con = db.DBConnection()
    main_db_con.upsert("tv_shows", new_value_dict, control_value_dict)

    helpers.update_anime_support()

    if self.imdb_id and self.imdb_info.get("year"):
        control_value_dict = {"indexer": self.indexer, "indexer_id": self.series_id}
        new_value_dict = self.imdb_info

        main_db_con = db.DBConnection()
        main_db_con.upsert("imdb_info", new_value_dict, control_value_dict)

    self.reset_dirty()