> You have to provide the following information whenever possible.

Exception when insert into materialized view

> A clear and concise description of what works not as it is supposed to.

**Does it reproduce on recent release?**

Reproduce on `ClickHouse 22.5.1.1 with revision 54462`

[The list of releases](https://github.com/ClickHouse/ClickHouse/blob/master/utils/list-versions/version_date.tsv)

**Enable crash reporting**

**How to reproduce**

* Which ClickHouse server version to use: ClickHouse 22.5.1.1 with revision 54462
* Which interface to use, if matters
* Non-default settings, if any
* `CREATE TABLE` statements for all tables involved
* Sample data for all these tables, use [clickhouse-obfuscator](https://github.com/ClickHouse/ClickHouse/blob/master/programs/obfuscator/Obfuscator.cpp#L42-L80) if necessary
* Queries to run that lead to unexpected result

---

1. Remove clickhouse data directory
2. Start clickhouse server
3. create the following tables

```
CREATE TABLE mt(a Int32, timestamp DateTime) ENGINE=MergeTree ORDER BY tuple();
CREATE MATERIALIZED VIEW mv ENGINE MergeTree ORDER BY tuple() AS SELECT count(a) AS count FROM mt;
```
4. Drop table mv

```
DROP TABLE mv
```

5. Terminate server by ctrl-c, then we can find `default.mv.UUID-OLD.sql` in `/metadata_dropped`
6. Start the server, a dependency of mt to UUID-OLD will be added by StorageMaterializedView:132 (might from metadata_dropped)
7. Recreate mv, and the server will add a dependency of mt to UUID-new-mv. Thus there are two dependencies in server, 'mv -> UUID-OLD' and 'mv -> UUID-new-mv`

```
CREATE MATERIALIZED VIEW mv ENGINE MergeTree ORDER BY tuple() AS SELECT count(a) AS count FROM mt;
```

8. Insert data into mt, a exception will be caused by the old dependency.

```
:) INSERT INTO mt VALUES (1, now());

INSERT INTO mt FORMAT Values

Query id: 06910fcc-ba7c-47a2-b821-0ff959988f59


0 rows in set. Elapsed: 0.024 sec.

Received exception from server (version 22.5.1):
Code: 60. DB::Exception: Received from localhost:9000. DB::Exception: Table default.mv (182c0880-5b8e-4972-aebc-b992d3654c06) doesn't exist. (UNKNOWN_TABLE)
```

> A clear and concise description of what you expected to happen.

**Error message and/or stacktrace**

> If applicable, add screenshots to help explain your problem.

```
Code: 60. DB::Exception: Received from localhost:9000. DB::Exception: Table default.mv (182c0880-5b8e-4972-aebc-b992d3654c06) doesn't exist. (UNKNOWN_TABLE)
```

**Additional context**

> Add any other context about the problem here.

assert(target_table_id.database_name == old_table_id.database_name);
        target_table_id.database_name = new_table_id.database_name;
    }
    const auto & select_query = metadata_snapshot->getSelectQuery();
    
    DatabaseCatalog::instance().updateDependency(select_query.select_table_id, old_table_id, select_query.select_table_id, getStorageID());
}
void StorageMaterializedView::shutdown()
{
    auto metadata_snapshot = getInMemoryMetadataPtr();
    const auto & select_query = metadata_snapshot->getSelectQuery();
    
    if (!select_query.select_table_id.empty())
        DatabaseCatalog::instance().removeDependency(select_query.select_table_id, getStorageID());
}
StoragePtr StorageMaterializedView::getTargetTable() const
{
    checkStackSize();
    return DatabaseCatalog::instance().getTable(target_table_id, getContext());
}
StoragePtr StorageMaterializedView::tryGetTargetTable() const
{
    checkStackSize();
    return DatabaseCatalog::instance().tryGetTable(target_table_id, getContext());
}
NamesAndTypesList StorageMaterializedView::getVirtuals() const
{
    return getTargetTable()->getVirtuals();
}
Strings StorageMaterializedView::getDataPaths() const