> 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.

{
    auto old_table_id = getStorageID();
    auto metadata_snapshot = getInMemoryMetadataPtr();
    bool from_atomic_to_atomic_database = old_table_id.hasUUID() && new_table_id.hasUUID();
    if (!from_atomic_to_atomic_database && has_inner_table && tryGetTargetTable())
    {
        auto new_target_table_name = generateInnerTableName(new_table_id);
        auto rename = std::make_shared<ASTRenameQuery>();
        ASTRenameQuery::Table from;
        assert(target_table_id.database_name == old_table_id.database_name);
        from.database = target_table_id.database_name;
        from.table = target_table_id.table_name;
        ASTRenameQuery::Table to;
        to.database = new_table_id.database_name;
        to.table = new_target_table_name;
        ASTRenameQuery::Element elem;
        elem.from = from;
        elem.to = to;
        rename->elements.emplace_back(elem);
        InterpreterRenameQuery(rename, getContext()).execute();
        target_table_id.database_name = new_table_id.database_name;
        target_table_id.table_name = new_target_table_name;
    }
    IStorage::renameInMemory(new_table_id);
    if (from_atomic_to_atomic_database && has_inner_table)
    {