# Buggy code
def get_old_channels(self):
    connection = sqlite3.connect(self.tribler_db)
    cursor = connection.cursor()

    channels = []
    for (
        id_,
        name,
        dispersy_cid,
        modified,
        nr_torrents,
        nr_favorite,
        nr_spam,
    ) in cursor.execute(self.select_channels_sql):
        if nr_torrents and nr_torrents > 0:
            channels.append(
                {
                    "id_": infohash_to_id(dispersy_cid),
                    # converting this to str is a workaround for python 2.7 'writable buffers not hashable'
                    # problem with Pony
                    "infohash": str(dispersy_cid),
                    "title": name or "",
                    "public_key": NULL_KEY,
                    "timestamp": final_timestamp(),
                    "origin_id": 0,
                    "size": 0,
                    "subscribed": False,
                    "status": LEGACY_ENTRY,
                    "num_entries": int(nr_torrents or 0),
                }
            )
    connection.close()
    return channels


# Traceback (RuntimeError)
Traceback (most recent call last):
File "TriblerGUI\event_request_manager.py", line 114, in on_read_data
RuntimeError: [Failure instance: Traceback: <class 'pony.orm.core.TransactionIntegrityError'>: Object TorrentMetadata[new:47] cannot be stored in the database. IntegrityError: UNIQUE constraint failed: ChannelNode.public_key, ChannelNode.id_
Tribler\Core\Modules\restapi\torrentinfo_endpoint.py:135:render_GET
Tribler\Core\Modules\restapi\torrentinfo_endpoint.py:109:on_file
lib\site-packages\twisted\internet\defer.py:459:callback
lib\site-packages\twisted\internet\defer.py:567:_startRunCallbacks
--- <exception caught here> ---
lib\site-packages\twisted\internet\defer.py:653:_runCallbacks
Tribler\Core\Modules\restapi\torrentinfo_endpoint.py:70:on_got_metainfo
<auto generated wrapper of add_ffa_from_tdef() function>:2:add_ffa_from_tdef
lib\site-packages\pony\orm\core.py:543:new_func
lib\site-packages\pony\orm\core.py:528:new_func
Tribler\Core\Modules\MetadataStore\OrmBindings\torrent_metadata.py:98:add_ffa_from_tdef
Tribler\Core\Modules\MetadataStore\OrmBindings\channel_node.py:213:from_dict
Tribler\Core\Modules\MetadataStore\OrmBindings\torrent_metadata.py:77:__init__
Tribler\Core\Modules\MetadataStore\OrmBindings\torrent_metadata.py:82:add_tracker
lib\site-packages\pony\orm\core.py:3950:get
lib\site-packages\pony\orm\core.py:4053:_find_one_
lib\site-packages\pony\orm\core.py:4112:_find_in_db_
lib\site-packages\pony\orm\core.py:934:_exec_sql
lib\site-packages\pony\orm\core.py:1797:prepare_connection_for_query_execution
lib\site-packages\pony\orm\core.py:1883:flush
lib\site-packages\pony\orm\core.py:5357:_save_
lib\site-packages\pony\orm\core.py:5201:_save_created_
lib\site-packages\pony\utils\utils.py:106:throw
]

# Fixed code
def get_old_channels(self):
    connection = sqlite3.connect(self.tribler_db)
    cursor = connection.cursor()

    channels = []
    for (
        id_,
        name,
        dispersy_cid,
        modified,
        nr_torrents,
        nr_favorite,
        nr_spam,
    ) in cursor.execute(self.select_channels_sql):
        if nr_torrents and nr_torrents > 0:
            channels.append(
                {
                    "id_": infohash_to_id(dispersy_cid),
                    # converting this to str is a workaround for python 2.7 'writable buffers not hashable'
                    # problem with Pony
                    "infohash": str(dispersy_cid),
                    "title": name or "",
                    "public_key": "",
                    "timestamp": final_timestamp(),
                    "origin_id": 0,
                    "size": 0,
                    "subscribed": False,
                    "status": LEGACY_ENTRY,
                    "num_entries": int(nr_torrents or 0),
                }
            )
    connection.close()
    return channels