在第一个迁移文件中创建的扩展在第二个迁移文件中不可用。
作者: nehalagrawal-hc创建于 2023年7月28日更新于 2026年9月23日
描述错误
以下是我的迁移文件的格式
000001_init_schema.up.sql
CREATE EXTENSION IF NOT EXISTS "uuid-ossp" WITH SCHEMA public;
COMMENT ON EXTENSION "uuid-ossp" IS 'generate universally unique identifiers (UUIDs)';
CREATE TABLE public.tenant (
id uuid NOT NULL,
created timestamp with time zone NOT NULL,
last_updated timestamp with time zone NOT NULL,
.....
.....
.....
);
ALTER TABLE ONLY public.tenant
ADD CONSTRAINT tenant_name_unq UNIQUE (name);000002_add_default_data.up.sql
INSERT INTO public.tenant ("id", "name", "type", "state", "created", "last_updated")
VALUES (uuid_generate_v4(), 'default', 'INTERNAL', 'NEW', now(), now())
ON CONFLICT ON CONSTRAINT tenant_name_unq
DO NOTHING;当我同时运行这两个迁移时
migrate -path ./migrations/ -database postgres://$POSTGRES_USER:$POSTGRES_PASSWORD@$POSTGRES_HOST:5432/$POSTGRES_DB?sslmode=disable -verbose up
它会抛出以下错误(我已经验证了 uuid_ossp 在 pg_extension 表中是否出现。)
2023/07/28 17:23:27 Read and execute 2/u add_default_data
2023/07/28 17:24:27 error: migration failed: function uuid_generate_v4() does not exist (column 9) in line 8:
(details: pq: function uuid_generate_v4() does not exist)但是,如果我分别运行这两个迁移,先运行
migrate -path ./migrations/ -database postgres://$POSTGRES_USER:$POSTGRES_PASSWORD@$POSTGRES_HOST:5432/$POSTGRES_DB?sslmode=disable -verbose up 1
然后运行
migrate -path ./migrations/ -database postgres://$POSTGRES_USER:$POSTGRES_PASSWORD@$POSTGRES_HOST:5432/$POSTGRES_DB?sslmode=disable -verbose up 2
它们都成功了,没有任何错误。 迁移版本 - 我使用来自 Docker hub 的 migrate/migrate Docker 镜像来运行这些迁移。
ARG MIGRATE_VERSION=v4.15.1
FROM migrate/migrate:${MIGRATE_VERSION}内容来源: golang-migrate/migrate