Skip to content

Commit

Permalink
Merge pull request #74 from k1LoW/primary-key-and-foreign-key
Browse files Browse the repository at this point in the history
Fix the bug that foreign key constraints are not listed in the document and ER diagram in the case of primary key and foreign key
  • Loading branch information
k1LoW committed Dec 8, 2018
2 parents 1d933d3 + 4bca805 commit e23c6a3
Show file tree
Hide file tree
Showing 41 changed files with 547 additions and 122 deletions.
4 changes: 2 additions & 2 deletions datasource/datasource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ var tests = []struct {
tableCount int
relationCount int
}{
{"my://root:mypass@localhost:33306/testdb", 7, 5},
{"pg://postgres:pgpass@localhost:55432/testdb?sslmode=disable", 8, 6},
{"my://root:mypass@localhost:33306/testdb", 8, 6},
{"pg://postgres:pgpass@localhost:55432/testdb?sslmode=disable", 9, 7},
{"json://../testdata/testdb.json", 7, 9},
}

Expand Down
3 changes: 2 additions & 1 deletion drivers/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,11 @@ LEFT JOIN
kcu.table_name,
kcu.constraint_name,
kcu.column_name,
(CASE WHEN c.column_key='PRI' THEN 'PRIMARY KEY'
(CASE WHEN c.column_key='PRI' AND kcu.referenced_table_name IS NULL THEN 'PRIMARY KEY'
WHEN c.column_key='UNI' THEN 'UNIQUE'
WHEN c.column_key='MUL' AND kcu.referenced_table_name IS NULL THEN 'UNIQUE'
WHEN c.column_key='MUL' AND kcu.referenced_table_name IS NOT NULL THEN 'FOREIGN KEY'
WHEN c.column_key='PRI' AND kcu.referenced_table_name IS NOT NULL THEN 'FOREIGN KEY'
ELSE 'UNKNOWN'
END) AS costraint_type
FROM information_schema.key_column_usage AS kcu
Expand Down
1 change: 1 addition & 0 deletions sample/adjust/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
| Name | Columns | Comment | Type |
| --------------------------------------------- | ------- | ------------------------------------------ | ---------- |
| [users](users.md) | 6 | Users table | BASE TABLE |
| [user_options](user_options.md) | 4 | User options table | BASE TABLE |
| [posts](posts.md) | 8 | Posts table | BASE TABLE |
| [comments](comments.md) | 6 | Comments<br>Multi-line<br>table<br>comment | BASE TABLE |
| [comment_stars](comment_stars.md) | 6 | | BASE TABLE |
Expand Down
Binary file modified sample/adjust/schema.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions sample/adjust/user_options.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# user_options

## Description

User options table

## Columns

| Name | Type | Default | Nullable | Children | Parents | Comment |
| ---------- | --------------------------- | ------- | -------- | -------- | ----------------- | ------- |
| user_id | integer | | false | | [users](users.md) | |
| show_email | boolean | false | false | | | |
| created | timestamp without time zone | | false | | | |
| updated | timestamp without time zone | | true | | | |

## Constraints

| Name | Type | Definition |
| ----------------------- | ----------- | ------------------------------------------------------------ |
| user_options_user_id_fk | FOREIGN KEY | FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE |
| user_options_pkey | PRIMARY KEY | PRIMARY KEY (user_id) |

## Indexes

| Name | Definition |
| ----------------- | ---------------------------------------------------------------------------------- |
| user_options_pkey | CREATE UNIQUE INDEX user_options_pkey ON public.user_options USING btree (user_id) |

## Relations

![er](user_options.png)

---

> Generated by [tbls](https://github.com/k1LoW/tbls)
Binary file added sample/adjust/user_options.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 8 additions & 8 deletions sample/adjust/users.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ Users table

## Columns

| Name | Type | Default | Nullable | Children | Parents | Comment |
| -------- | --------------------------- | --------------------------------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------------- | ------- | -------------------- |
| id | integer | nextval('users_id_seq'::regclass) | false | [posts](posts.md) [comments](comments.md) [comment_stars](comment_stars.md) [administrator.blogs](administrator.blogs.md) [logs](logs.md) | | |
| username | varchar(50) | | false | | | |
| password | varchar(50) | | false | | | |
| email | varchar(355) | | false | | | ex. user@example.com |
| created | timestamp without time zone | | false | | | |
| updated | timestamp without time zone | | true | | | |
| Name | Type | Default | Nullable | Children | Parents | Comment |
| -------- | --------------------------- | --------------------------------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------- | -------------------- |
| id | integer | nextval('users_id_seq'::regclass) | false | [user_options](user_options.md) [posts](posts.md) [comments](comments.md) [comment_stars](comment_stars.md) [administrator.blogs](administrator.blogs.md) [logs](logs.md) | | |
| username | varchar(50) | | false | | | |
| password | varchar(50) | | false | | | |
| email | varchar(355) | | false | | | ex. user@example.com |
| created | timestamp without time zone | | false | | | |
| updated | timestamp without time zone | | true | | | |

## Constraints

Expand Down
Binary file modified sample/adjust/users.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions sample/mysql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
| [logs](logs.md) | 7 | audit log table | BASE TABLE |
| [post_comments](post_comments.md) | 7 | post and comments View table | VIEW |
| [posts](posts.md) | 7 | Posts table | BASE TABLE |
| [user_options](user_options.md) | 4 | User options table | BASE TABLE |
| [users](users.md) | 6 | Users table | BASE TABLE |

## Relations
Expand Down
Binary file modified sample/mysql/schema.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
51 changes: 51 additions & 0 deletions sample/mysql/user_options.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# user_options

## Description

User options table

<details>
<summary><strong>Table Definition</strong></summary>

```sql
CREATE TABLE `user_options` (
`user_id` int(11) NOT NULL,
`show_email` tinyint(1) NOT NULL DEFAULT '0',
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`updated` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`user_id`),
CONSTRAINT `user_options_user_id_fk` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='User options table'
```

</details>

## Columns

| Name | Type | Default | Nullable | Children | Parents | Comment |
| ---- | ---- | ------- | -------- | -------- | ------- | ------- |
| user_id | int(11) | | false | | [users](users.md) | |
| show_email | tinyint(1) | 0 | false | | | |
| created | timestamp | CURRENT_TIMESTAMP | false | | | |
| updated | timestamp | 0000-00-00 00:00:00 | false | | | |

## Constraints

| Name | Type | Definition |
| ---- | ---- | ---------- |
| PRIMARY | PRIMARY KEY | PRIMARY KEY (user_id) |
| user_options_user_id_fk | FOREIGN KEY | FOREIGN KEY (user_id) REFERENCES users (id) |

## Indexes

| Name | Definition |
| ---- | ---------- |
| PRIMARY | PRIMARY KEY (user_id) USING BTREE |

## Relations

![er](user_options.png)

---

> Generated by [tbls](https://github.com/k1LoW/tbls)
Binary file added sample/mysql/user_options.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion sample/mysql/users.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ CREATE TABLE `users` (

| Name | Type | Default | Nullable | Children | Parents | Comment |
| ---- | ---- | ------- | -------- | -------- | ------- | ------- |
| id | int(11) | | false | [comment_stars](comment_stars.md) [comments](comments.md) [posts](posts.md) [logs](logs.md) | | |
| id | int(11) | | false | [comment_stars](comment_stars.md) [comments](comments.md) [posts](posts.md) [user_options](user_options.md) [logs](logs.md) | | |
| username | varchar(50) | | false | | | |
| password | varchar(50) | | false | | | |
| email | varchar(355) | | false | | | ex. user@example.com |
Expand Down
Binary file modified sample/mysql/users.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions sample/mysql8/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
| [logs](logs.md) | 7 | audit log table | BASE TABLE |
| [post_comments](post_comments.md) | 7 | post and comments View table | VIEW |
| [posts](posts.md) | 7 | Posts table | BASE TABLE |
| [user_options](user_options.md) | 4 | User options table | BASE TABLE |
| [users](users.md) | 6 | Users table | BASE TABLE |

## Relations
Expand Down
Binary file modified sample/mysql8/schema.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
51 changes: 51 additions & 0 deletions sample/mysql8/user_options.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# user_options

## Description

User options table

<details>
<summary><strong>Table Definition</strong></summary>

```sql
CREATE TABLE `user_options` (
`user_id` int(11) NOT NULL,
`show_email` tinyint(1) NOT NULL DEFAULT '0',
`created` timestamp NOT NULL,
`updated` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`user_id`),
CONSTRAINT `user_options_user_id_fk` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='User options table'
```

</details>

## Columns

| Name | Type | Default | Nullable | Children | Parents | Comment |
| ---- | ---- | ------- | -------- | -------- | ------- | ------- |
| user_id | int(11) | | false | | [users](users.md) | |
| show_email | tinyint(1) | 0 | false | | | |
| created | timestamp | | false | | | |
| updated | timestamp | | true | | | |

## Constraints

| Name | Type | Definition |
| ---- | ---- | ---------- |
| PRIMARY | PRIMARY KEY | PRIMARY KEY (user_id) |
| user_options_user_id_fk | FOREIGN KEY | FOREIGN KEY (user_id) REFERENCES users (id) |

## Indexes

| Name | Definition |
| ---- | ---------- |
| PRIMARY | PRIMARY KEY (user_id) USING BTREE |

## Relations

![er](user_options.png)

---

> Generated by [tbls](https://github.com/k1LoW/tbls)
Binary file added sample/mysql8/user_options.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion sample/mysql8/users.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ CREATE TABLE `users` (

| Name | Type | Default | Nullable | Children | Parents | Comment |
| ---- | ---- | ------- | -------- | -------- | ------- | ------- |
| id | int(11) | | false | [comment_stars](comment_stars.md) [comments](comments.md) [posts](posts.md) [logs](logs.md) | | |
| id | int(11) | | false | [comment_stars](comment_stars.md) [comments](comments.md) [posts](posts.md) [user_options](user_options.md) [logs](logs.md) | | |
| username | varchar(50) | | false | | | |
| password | varchar(50) | | false | | | |
| email | varchar(355) | | false | | | ex. user@example.com |
Expand Down
Binary file modified sample/mysql8/users.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions sample/postgres/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
| Name | Columns | Comment | Type |
| ---- | ------- | ------- | ---- |
| [users](users.md) | 6 | Users table | BASE TABLE |
| [user_options](user_options.md) | 4 | User options table | BASE TABLE |
| [posts](posts.md) | 8 | Posts table | BASE TABLE |
| [comments](comments.md) | 6 | Comments<br>Multi-line<br>table<br>comment | BASE TABLE |
| [comment_stars](comment_stars.md) | 6 | | BASE TABLE |
Expand Down
Binary file modified sample/postgres/schema.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions sample/postgres/user_options.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# user_options

## Description

User options table

## Columns

| Name | Type | Default | Nullable | Children | Parents | Comment |
| ---- | ---- | ------- | -------- | -------- | ------- | ------- |
| user_id | integer | | false | | [users](users.md) | |
| show_email | boolean | false | false | | | |
| created | timestamp without time zone | | false | | | |
| updated | timestamp without time zone | | true | | | |

## Constraints

| Name | Type | Definition |
| ---- | ---- | ---------- |
| user_options_user_id_fk | FOREIGN KEY | FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE |
| user_options_pkey | PRIMARY KEY | PRIMARY KEY (user_id) |

## Indexes

| Name | Definition |
| ---- | ---------- |
| user_options_pkey | CREATE UNIQUE INDEX user_options_pkey ON public.user_options USING btree (user_id) |

## Relations

![er](user_options.png)

---

> Generated by [tbls](https://github.com/k1LoW/tbls)
Binary file added sample/postgres/user_options.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion sample/postgres/users.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Users table

| Name | Type | Default | Nullable | Children | Parents | Comment |
| ---- | ---- | ------- | -------- | -------- | ------- | ------- |
| id | integer | nextval('users_id_seq'::regclass) | false | [posts](posts.md) [comments](comments.md) [comment_stars](comment_stars.md) [administrator.blogs](administrator.blogs.md) [logs](logs.md) | | |
| id | integer | nextval('users_id_seq'::regclass) | false | [user_options](user_options.md) [posts](posts.md) [comments](comments.md) [comment_stars](comment_stars.md) [administrator.blogs](administrator.blogs.md) [logs](logs.md) | | |
| username | varchar(50) | | false | | | |
| password | varchar(50) | | false | | | |
| email | varchar(355) | | false | | | ex. user@example.com |
Expand Down
Binary file modified sample/postgres/users.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions sample/sqlite/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
| Name | Columns | Comment | Type |
| ---- | ------- | ------- | ---- |
| [users](users.md) | 6 | | table |
| [user_options](user_options.md) | 4 | | table |
| [posts](posts.md) | 6 | | table |
| [comments](comments.md) | 6 | | table |
| [comment_stars](comment_stars.md) | 6 | | table |
Expand Down
Binary file modified sample/sqlite/schema.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 42 additions & 0 deletions sample/sqlite/user_options.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# user_options

## Description

<details>
<summary><strong>Table Definition</strong></summary>

```sql
CREATE TABLE user_options (
user_id INTEGER PRIMARY KEY,
show_email INTEGER NOT NULL DEFAULT 0,
created NUMERIC NOT NULL,
updated NUMERIC,
CONSTRAINT user_options_user_id_fk FOREIGN KEY(user_id) REFERENCES users(id) MATCH NONE ON UPDATE NO ACTION ON DELETE CASCADE
)
```

</details>

## Columns

| Name | Type | Default | Nullable | Children | Parents | Comment |
| ---- | ---- | ------- | -------- | -------- | ------- | ------- |
| user_id | INTEGER | | true | | [users](users.md) | |
| show_email | INTEGER | 0 | false | | | |
| created | NUMERIC | | false | | | |
| updated | NUMERIC | | true | | | |

## Constraints

| Name | Type | Definition |
| ---- | ---- | ---------- |
| user_id | PRIMARY KEY | PRIMARY KEY (user_id) |
| - (Foreign key ID: 0) | FOREIGN KEY | FOREIGN KEY (user_id) REFERENCES users (id) ON UPDATE NO ACTION ON DELETE CASCADE MATCH NONE |

## Relations

![er](user_options.png)

---

> Generated by [tbls](https://github.com/k1LoW/tbls)
Binary file added sample/sqlite/user_options.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion sample/sqlite/users.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ CREATE TABLE users (

| Name | Type | Default | Nullable | Children | Parents | Comment |
| ---- | ---- | ------- | -------- | -------- | ------- | ------- |
| id | INTEGER | | true | [posts](posts.md) [comments](comments.md) [comment_stars](comment_stars.md) [logs](logs.md) | | |
| id | INTEGER | | true | [user_options](user_options.md) [posts](posts.md) [comments](comments.md) [comment_stars](comment_stars.md) [logs](logs.md) | | |
| username | TEXT | | false | | | |
| password | TEXT | | false | | | |
| email | TEXT | | false | | | |
Expand Down
Binary file modified sample/sqlite/users.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions sample/svg/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
| [logs](logs.md) | 7 | audit log table | BASE TABLE |
| [post_comments](post_comments.md) | 7 | post and comments View table | VIEW |
| [posts](posts.md) | 7 | Posts table | BASE TABLE |
| [user_options](user_options.md) | 4 | User options table | BASE TABLE |
| [users](users.md) | 6 | Users table | BASE TABLE |

## Relations
Expand Down
Loading

0 comments on commit e23c6a3

Please # to comment.