我有一个包含两个数据库的项目:PostgreSQL 和 Clickhouse。
为了与 Clickhouse 交互,我使用
django-clickhouse-backend
库。很长一段时间,我没有使用该数据库创建的迁移,而是使用原始 SQL 创建表。现在是时候应用之前的所有迁移了,但我遇到了问题。
为了避免所有冲突,我必须对现有的迁移文件进行大量编辑,之后我在本地建模并检查这些迁移在现有数据库中的应用,一切正常。但是当我尝试在生产服务器上应用这些迁移时,出现以下错误:
# python manage.py migrate --database clickhouse --fake-initial
Operations to perform:
Apply all migrations: admin, auth, authtoken, celery_app, contenttypes, django_dramatiq, edl, sessions, users
Running migrations:
Applying contenttypes.0001_initial...Traceback (most recent call last):
File "/usr/local/lib/python3.11/site-packages/clickhouse_driver/dbapi/cursor.py", line 111, in execute
response = execute(
^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/clickhouse_backend/driver/client.py", line 53, in execute
rv = self.process_ordinary_query(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/clickhouse_driver/client.py", line 571, in process_ordinary_query
return self.receive_result(with_column_types=with_column_types,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/clickhouse_driver/client.py", line 204, in receive_result
return result.get_result()
^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/clickhouse_driver/result.py", line 50, in get_result
for packet in self.packet_generator:
File "/usr/local/lib/python3.11/site-packages/clickhouse_driver/client.py", line 220, in packet_generator
packet = self.receive_packet()
^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/clickhouse_driver/client.py", line 237, in receive_packet
raise packet.exception
clickhouse_driver.errors.ServerException: Code: 57.
DB::Exception: Table edl.django_migrations already exists. Stack trace:
0. DB::Exception::Exception(DB::Exception::MessageMasked&&, int, bool) @ 0x000000000cdd11b7 in /usr/bin/clickhouse
1. DB::Exception::Exception<String&, String, String>(int, FormatStringHelperImpl<std::type_identity<String&>::type, std::type_identity<String>::type, std::type_identity<String>::type>, String&, String&&, String&&) @ 0x0000000006c9791e in /usr/bin/clickhouse
2. DB::InterpreterCreateQuery::doCreateTable(DB::ASTCreateQuery&, DB::InterpreterCreateQuery::TableProperties const&, std::unique_ptr<DB::DDLGuard, std::default_delete<DB::DDLGuard>>&) @ 0x00000000125e309f in /usr/bin/clickhouse
3. DB::InterpreterCreateQuery::createTable(DB::ASTCreateQuery&) @ 0x00000000125d9ddf in /usr/bin/clickhouse
4. DB::InterpreterCreateQuery::execute() @ 0x00000000125e725e in /usr/bin/clickhouse
5. DB::executeQueryImpl(char const*, char const*, std::shared_ptr<DB::Context>, bool, DB::QueryProcessingStage::Enum, DB::ReadBuffer*) @ 0x0000000012be45f5 in /usr/bin/clickhouse
6. DB::executeQuery(String const&, std::shared_ptr<DB::Context>, bool, DB::QueryProcessingStage::Enum) @ 0x0000000012bde2fe in /usr/bin/clickhouse
7. DB::TCPHandler::runImpl() @ 0x0000000013aa32c9 in /usr/bin/clickhouse
8. DB::TCPHandler::run() @ 0x0000000013ab7c79 in /usr/bin/clickhouse
9. Poco::Net::TCPServerConnection::start() @ 0x000000001658ffb4 in /usr/bin/clickhouse
10. Poco::Net::TCPServerDispatcher::run() @ 0x00000000165911b1 in /usr/bin/clickhouse
11. Poco::PooledThread::run() @ 0x000000001669c8c7 in /usr/bin/clickhouse
12. Poco::ThreadImpl::runnableEntry(void*) @ 0x000000001669ab5c in /usr/bin/clickhouse
13. ? @ 0x00007fa57718bac3 in ?
14. ? @ 0x00007fa57721da40 in ?
事实上,
edl.django_migrations
表最初并不存在,但在执行此命令后创建了。
我尝试执行
python mamage.py migrate --database clickhouse --fake
,但没有帮助。
我尝试将
django_migrations
表从PostgreSQL迁移到Clickhouse并执行python mamage.py migrate --database clickhouse --fake
,但也没有给出结果。
我在 clickhouse 版本 4.* 上也有同样的东西。我降级到版本23.7.2.25,问题解决了。