# SQL Question

**URL:** <https://forums.plex.tv/t/sql-question/858069>\
**Category:** Plex Media Server\
**Tags:** server-windows\
**Created:** [October 29, 2023, 9:00pm UTC](https://forums.plex.tv/t/sql-question/858069 "2023-10-29T21:00:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dbirch](https://avatars.discourse-cdn.com/v4/letter/d/f19dbf/32.png) [@dbirch](https://forums.plex.tv/u/dbirch)\
**Post date:** [October 29, 2023, 9:00pm UTC](https://forums.plex.tv/t/sql-question/858069/1 "2023-10-29T21:00:24Z")

</div>

I am asking about something I know is a bad idea, but I know the risks and have backups.

I had a drive get disconnected, and Plex removed/re-added the movies.

I am looking for a sql command to set the file last modified value as the date/time added to plex.

---

<div class="post-metadata">

**Author:** ![BanzaiInstitute](https://sea1.discourse-cdn.com/plex/user_avatar/forums.plex.tv/banzaiinstitute/32/293273_2.png) [@BanzaiInstitute](https://forums.plex.tv/u/BanzaiInstitute)\
**Post date:** [October 30, 2023, 12:10am UTC](https://forums.plex.tv/t/sql-question/858069/2 "2023-10-30T00:10:18Z")

</div>

If you turn off the option to automatically empty the trash for the library, then a temporary disconnection won’t matter.

---

<div class="post-metadata">

**Author:** ![dbirch](https://avatars.discourse-cdn.com/v4/letter/d/f19dbf/32.png) [@dbirch](https://forums.plex.tv/u/dbirch)\
**Post date:** [October 30, 2023, 12:15am UTC](https://forums.plex.tv/t/sql-question/858069/3 "2023-10-30T00:15:06Z")

</div>

Yeah I know, I had it off, it got turned back on somehow, and I have since re-disabled it.

---

<div class="post-metadata">

**Author:** ![DTR](https://avatars.discourse-cdn.com/v4/letter/d/9fc348/32.png) [@DTR](https://forums.plex.tv/u/DTR)\
**Post date:** [October 30, 2023, 1:38am UTC](https://forums.plex.tv/t/sql-question/858069/4 "2023-10-30T01:38:05Z")

</div>

Do you have any [recent backups](https://support.plex.tv/articles/202485658-restore-a-database-backed-up-via-scheduled-tasks/) you can restore from? That might be easier than the SQL route.

Outside of that, the following should set the “date added” to the file date for _all_ items:

```sql
UPDATE metadata_items SET added_at=created_at;

```

If it’s just a specific library you want to update, you could specify the `library_section_id`:

```sql
UPDATE metadata_items SET added_at=created_at WHERE library_section_id=2;

```

Or based on the file path (`%` is a wildcard, so the following looks for all file paths that start with `/mnt/driveA/`):

```sql
UPDATE metadata_items SET added_at=created_at WHERE id IN (
    SELECT m.id FROM metadata_items m
        INNER JOIN media_items mi ON mi.metadata_item_id=m.id
        INNER JOIN media_parts p ON p.media_item_id=mi.id
    WHERE p.file LIKE "/mnt/driveA/%");

```

You’ll have to use [Plex SQLite](https://support.plex.tv/articles/repair-a-corrupted-database/#toc-1) to modify `metadata_items`, since it uses custom extensions that aren’t part of the standard sqlite3 distribution.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/plex/original/3X/2/a/2acb9765406f63293d357b4ec509ec39aa28f2ad.png) [@system](https://forums.plex.tv/u/system)\
**Post date:** [January 28, 2024, 1:38am UTC](https://forums.plex.tv/t/sql-question/858069/5 "2024-01-28T01:38:24Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
