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

inferring PK from existing schema #704

Closed
dvdplm opened this Issue Feb 14, 2017 · 4 comments

Comments

Projects
None yet
2 participants
@dvdplm

dvdplm commented Feb 14, 2017

Using the table definition below and the infer_table_from_schema! macro I'm getting an odd error: error: proc-macro derive panicked.

CREATE TABLE stuffs (
    id bigint NOT NULL,
    uuid character varying(255),
    account_id bigint
   );
CREATE SEQUENCE stuffs_id_seq1
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;
    
ALTER SEQUENCE stuffs_id_seq1 OWNED BY stuffs.id;
ALTER TABLE ONLY stuffs ALTER COLUMN id SET DEFAULT nextval('stuffs_id_seq1'::regclass);
ALTER TABLE ONLY stuffs
    ADD CONSTRAINT stuffs_pkey1 PRIMARY KEY (id);
CREATE INDEX stuffs_account_id_idx ON stuffs USING btree (account_id);

The above SQL is an extract from the dump that pg_dump produces so it's perhaps not the way a human would have written it but results in a perfectly valid table. This SQL adds rows and increments the PK:

INSERT INTO stuffs ("uuid", "account_id") VALUES('abc123', 999);
INSERT INTO stuffs ("uuid", "account_id") VALUES('def456', 888);

Rust version:

$ rustc -V
rustc 1.17.0-nightly (956e2bcba 2017-02-12)

Cargo.toml:

[package]
name = "diesel_demo"
version = "0.1.0"
authors = ["David Palm <dvdplm@gmail.com>"]

[dependencies]
diesel = { version = "0.10.1", features = ["postgres"] }
diesel_codegen = { version = "0.10.1", features = ["postgres"] }
dotenv = "0.8.0"

[replace]
"diesel:0.10.1" = { git = " https://github.com/diesel-rs/diesel.git" }
"diesel_codegen:0.10.1" = { git = " https://github.com/diesel-rs/diesel.git" }
"diesel_infer_schema:0.10.1" = { git = " https://github.com/diesel-rs/diesel.git" }

The code that runs is essentially the "getting_started_step_1" without any migrations generated as I'm trying to see how Diesel and Rust performs on my existing schema.

Full error output:

error: proc-macro derive panicked
 --> src/schema.rs:2:1
  |
2 | infer_table_from_schema!("dotenv:DATABASE_URL", "stuffs");
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = help: message: Could not infer table stuffs: StringError("Diesel only supports tables with primary keys. Table stuffs has no primary key")
  = note: this error originates in a macro outside of the current crate

error: Could not compile `diesel_demo`.

Caused by:
  process didn't exit successfully: `rustc --crate-name diesel_demo src/lib.rs --crate-type lib --emit=dep-info,link -C debuginfo=2 -C metadata=4534e1ff4f2f0cef -C extra-filename=-4534e1ff4f2f0cef --out-dir /Users/dvd/dev/rust/diesel_demo/target/debug/deps -L dependency=/Users/dvd/dev/rust/diesel_demo/target/debug/deps --extern diesel=/Users/dvd/dev/rust/diesel_demo/target/debug/deps/libdiesel-647af581b82e4074.rlib --extern diesel_codegen=/Users/dvd/dev/rust/diesel_demo/target/debug/deps/libdiesel_codegen-2fc13b4e395fcd1e.dylib --extern dotenv=/Users/dvd/dev/rust/diesel_demo/target/debug/deps/libdotenv-75d8ce5725f9d0e8.rlib -L native=/usr/local/Cellar/postgresql/9.6beta2/lib` (exit code: 101)
@sgrif

This comment has been minimized.

Member

sgrif commented Feb 14, 2017

I am able to reproduce the issue.

@sgrif

This comment has been minimized.

Member

sgrif commented Feb 14, 2017

I've identified the issue and am working on a solution. The issue only occurs when no schema name is specified on the table. As a workaround, you can write infer_table_from_schema!("dotenv:DATABASE_URL", "public.stuffs")

@sgrif

This comment has been minimized.

Member

sgrif commented Feb 14, 2017

I still need to look into the issue you were seeing on 0.10.1, as that should have been fixed. The issue with the primary key lookup was only an issue on master, not the released version (and we would have shipped with that bug in place had you not stumbled onto it)

sgrif added a commit that referenced this issue Feb 14, 2017

Fix `infer_table_from_schema!` when no schema name is given
When no schema is given, we are incorrectly querying `WHERE schema_name
= NULL`, which is nonsense and not what we want to write. It appears
this went uncaught because `infer_schema!` always gives a schema name to
`infer_table_from_schema!`

Fixes #704
@dvdplm

This comment has been minimized.

dvdplm commented Feb 15, 2017

Wow, that was fast! :)

@sgrif sgrif closed this in #705 Feb 15, 2017

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