Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ERROR: relation "user" does not exist while running diesel migration/print-schema #3673

Closed
3 tasks done
df51d opened this issue Jun 30, 2023 · 2 comments · Fixed by #3676
Closed
3 tasks done

ERROR: relation "user" does not exist while running diesel migration/print-schema #3673

df51d opened this issue Jun 30, 2023 · 2 comments · Fixed by #3676

Comments

@df51d
Copy link

df51d commented Jun 30, 2023

Setup

My pg dump

Versions

  • Rust:
    cargo 1.70.0 (ec8a8a0ca 2023-04-25)
    rustc 1.70.0 (90c541806 2023-05-31)
  • Diesel:
    diesel 2.1.0
  • Database:
    postgresql 15.3-1.pgdg120+1
  • Operating System
    ArcoLinux kernel 6.3.8-arch1-1

Feature Flags

cargo install diesel_cli --no-
default-features --features "postgres sqlite"

Problem Description

I encountered an error while running diesel print-schema/diesel migration run on my old database after installing the diesel-cli. I was attempting to switch from a TypeScript project that utilized Prisma as the database driver to a Rust project with Diesel as the database driver. Since starting from scratch and recreating everything would be a significant amount of work, my intention was to reuse my existing database. However, I faced difficulties during the process.

What are you trying to accomplish?

diesel print-schema

What is the actual output?

relation "user" does not exist

Steps to reproduce

with prisma installed

docker pull postgres

docker run -e POSTGRES_PASSWORD=postgres -p 5432:5432 -d postgres

echo 'DATABASE_URL="postgresql://postgres:postgres@localhost:5432/db"' > .env

prisma init

echo 'model User {\n\tid Int @id @default(autoincrement())\n}' >> prisma/schema.prisma

prisma db push

diesel print-schema

without prisma installed

docker pull postgres

docker run -e POSTGRES_PASSWORD=postgres -p 5432:5432 -d postgres

echo 'DATABASE_URL="postgresql://postgres:postgres@localhost:5432/db"' > .env

diesel setup

mkdir migrations/user

echo "CREATE TABLE public.\"User\" (
    id integer NOT NULL
);
ALTER TABLE public.\"User\" OWNER TO postgres;
CREATE SEQUENCE public.\"User_id_seq\"
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;
ALTER TABLE public.\"User_id_seq\" OWNER TO postgres;
ALTER SEQUENCE public.\"User_id_seq\" OWNED BY public.\"User\".id;
ALTER TABLE ONLY public.\"User\" ALTER COLUMN id SET DEFAULT nextval('public.\"User_id_seq\"'::regclass);
SELECT pg_catalog.setval('public.\"User_id_seq\"', 1, false);
ALTER TABLE ONLY public.\"User\"
    ADD CONSTRAINT \"User_pkey\" PRIMARY KEY (id);" > migrations/user/up.sql
    
diesel migration run

Temporary fix:

Making all tables lowercase seems to fix the problem:

working migration

--- this works just fine /migrations/user/up.sql
CREATE TABLE public."user" (
    id integer NOT NULL
);
ALTER TABLE public."user" OWNER TO postgres;
CREATE SEQUENCE public."user_id_seq"
    AS integer
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;
ALTER TABLE public."user_id_seq" OWNER TO postgres;
ALTER SEQUENCE public."user_id_seq" OWNED BY public."user".id;
ALTER TABLE ONLY public."user" ALTER COLUMN id SET DEFAULT nextval('public."user_id_seq"'::regclass);
SELECT pg_catalog.setval('public."user_id_seq"', 1, false);
ALTER TABLE ONLY public."user"
    ADD CONSTRAINT "user_pkey" PRIMARY KEY (id);

working prisma

model user {
  id Int @id @default(autoincrement())
}

Checklist

  • This issue can be reproduced on Rust's stable channel. (Your issue will be
    closed if this is not the case)
  • This issue can be reproduced without requiring a third party crate
@df51d df51d added the bug label Jun 30, 2023
weiznich added a commit to weiznich/diesel that referenced this issue Jul 4, 2023
We missed a quote in a table name before casting the table name to
`regclass`. This causes postgres to coerce the table name to lowercase,
which causes the query to seek for a non-existing table. This fixes diesel-rs#3673.
weiznich added a commit to weiznich/diesel that referenced this issue Jul 4, 2023
We missed a quote in a table name before casting the table name to
`regclass`. This causes postgres to coerce the table name to lowercase,
which causes the query to seek for a non-existing table. This fixes diesel-rs#3673.
weiznich added a commit to weiznich/diesel that referenced this issue Jul 4, 2023
We missed a quote in a table name before casting the table name to
`regclass`. This causes postgres to coerce the table name to lowercase,
which causes the query to seek for a non-existing table. This fixes diesel-rs#3673.
weiznich added a commit to weiznich/diesel that referenced this issue Jul 5, 2023
We missed a quote in a table name before casting the table name to
`regclass`. This causes postgres to coerce the table name to lowercase,
which causes the query to seek for a non-existing table. This fixes diesel-rs#3673.
@dsp
Copy link
Contributor

dsp commented Jul 19, 2023

I am curious if there is a possibility to get a point release with the fixed version. At the moment it does affects some of my projects and the last diesel release is 3 months old. Happy to help if there is a way (e.g backporting the patch). For now I’ll run nightly.

@weiznich
Copy link
Member

@dsp I plan to do a patch release for this as soon as a couple of other issues are fixed as well. This includes #3579 and #3643. I cannot comment on how long that takes because it really depends on how much contributions there are and how much time I personally find to work on that kind of stuff. Any help there would be great.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants