Electron100 / butane

An ORM for Rust with a focus on simplicity and on writing Rust, not SQL
Apache License 2.0
91 stars 13 forks source link

Database schema generation doesn't create relations #21

Closed MrJico closed 10 months ago

MrJico commented 1 year ago

First, I would like to thank you for this beautifull 😍project😍.

so i oppened this issue becuase i have tested this crate on two diffrent setups and both with backends(sqllite, pg). but the generated sql code for the relations is ether completly broken or there is something wrong with my setup.

basicly i copy pasted the getting started page and made the migrations. which in theory should generate all the necessary sql code for the relation ships. but it does not.

here is an example of the sql output: ⬇️

CREATE TABLE Post (
id SERIAL NOT NULL PRIMARY KEY,
title TEXT NOT NULL,
body TEXT NOT NULL,
published BOOLEAN NOT NULL,
blog BIGINT NOT NULL,
byline TEXT 
);
CREATE TABLE Tag (
tag TEXT NOT NULL PRIMARY KEY
);
CREATE TABLE Blog (
id BIGSERIAL NOT NULL PRIMARY KEY,
name TEXT NOT NULL
);
CREATE TABLE Post_tags_Many (
owner INTEGER NOT NULL,
has TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS butane_migrations (
name TEXT NOT NULL PRIMARY KEY
);

and here in this image visualized with dbeaver: image

yet there are not relations(foreign keys, reffrences) generated for the tables.

here an example of what the generated sql code should be: ⬇️

CREATE TABLE Blog (
    id BIGSERIAL NOT NULL PRIMARY KEY,
    name TEXT NOT NULL
);

CREATE TABLE Post (
    id SERIAL NOT NULL PRIMARY KEY,
    title TEXT NOT NULL,
    body TEXT NOT NULL,
    published BOOLEAN NOT NULL,
    blog INTEGER REFERENCES Blog(id),
    byline TEXT
);

CREATE TABLE TAG (
    tag TEXT NOT NULL PRIMARY KEY
);

CREATE TABLE Post_tags_Many (
    owner SERIAL REFERENCES Post(id),
    has TEXT REFERENCES Tag(tag),
    PRIMARY KEY (owner, has)
);

CREATE TABLE IF NOT EXISTS butane_migrations (
name TEXT NOT NULL PRIMARY KEY
);

and in this image visualized with dbeaver:

image

please keep in mind that in the example above i have not included any constraints or onupdate and ondelete and they are not generated ether.

P.S: for the example out put above, butane is version 0.5.0 and features default and pg are enabled.

MrJico commented 1 year ago

Note : foreign key constraints in db, cascade setting are planned in the roadmap found here roadmap

Electron100 commented 1 year ago

Thanks for filing. I'm going to edit the title to be more specific about what exactly doesn't get generated.

MrJico commented 1 year ago

I think Database schema generation doesn't create relations would be a better name for the issue

jayvdb commented 9 months ago

See https://github.com/Electron100/butane/issues/166 for the next part of this - cascading the delete.