cornucopia-rs / cornucopia

Generate type-checked Rust from your PostgreSQL.
Other
755 stars 31 forks source link

Issue with a null field in command line cornucopia #225

Closed massimo79m closed 7 months ago

massimo79m commented 8 months ago

I have a table CREATE TABLE IF NOT EXISTS dataset_dumps.huge_us_stocks_values ( id bigint NOT NULL DEFAULT nextval('dataset_dumps.huge_us_stocks_values_id_seq'::regclass), id_product bigint NOT NULL, date timestamp with time zone NOT NULL, open numeric(30,16) NOT NULL, high numeric(30,16) NOT NULL, low numeric(30,16) NOT NULL, close numeric(30,16) NOT NULL, volume bigint NOT NULL, openint bigint, CONSTRAINT huge_us_stocks_values_pkey PRIMARY KEY (id_product), CONSTRAINT "id_product-products_id" FOREIGN KEY (id_product) REFERENCES dataset_dumps.huge_us_stocks_products (id) MATCH FULL ON UPDATE NO ACTION ON DELETE NO ACTION )

and a query --: Huge_us_stocks_value() --! huge_us_stocks_values_insert(openint?) : (id) INSERT INTO dataset_dumps.huge_us_stocks_values (id_product, date, open, high, low, close, volume, openint) VALUES (:id_product, :date, :open, :high, :low, :close, :volume, :openint) RETURNING id; --! huge_us_stocks_product_select_id_product(openint?) : Huge_us_stocks_value SELECT id, date, open, high, low, close, volume, openint name FROM dataset_dumps.huge_us_stocks_values WHERE id_product=:id_product;

the openint is "?" because it can be null

when i run "cornucopia --sync --serialize live "postgres...."" from command line it returns "Error: × unknown field ╭─[queries/ds_huge_us_stocks_values.sql:3:1] 3 │ INSERT INTO dataset_dumps.huge_us_stocks_values (id_product, date, open, high, low, close, volume, openint) VALUES (:id_product, :date, :open, :high, :low, :close, :volume, :openint) RETURNING id; 4 │ --! huge_us_stocks_product_select_id_product(openint?) : Huge_us_stocks_value · ───┬─── · ╰── no field with this name was found 5 │ SELECT id, date, open, high, low, close, volume, openint name FROM dataset_dumps.huge_us_stocks_values WHERE id_product=:id_product; ╰──── help: use one of those names: id_product"

in the line "5", that report the SELECT, it returns a strange thing: "openint name" while it should be just"openint".

I think it is a bug

Virgiel commented 7 months ago

For the query huge_us_stocks_product_select_id_product you wrote :

--! huge_us_stocks_product_select_id_product(openint?) : Huge_us_stocks_value

which mean a query with a nullable argument openint. But for this query openint is a nullable column and must therefore be declared after : as :

--! huge_us_stocks_product_select_id_product: (openint?) 

or in your case :

--: Huge_us_stocks_value(openint?)
--! huge_us_stocks_product_select_id_product: Huge_us_stocks_value 
massimo79m commented 7 months ago

I apologize, the problem was in the query: "SELECT id, date, open, high, low, close, volume, openint name"

that "name" at the end! Thank you for the help!