Skip to content

Commit

Permalink
Merge pull request #3917 from tomperr/feature/3910_er_unique_key
Browse files Browse the repository at this point in the history
feat(er): add unique key
  • Loading branch information
knsv committed Dec 16, 2022
2 parents 1bf636d + c7f085a commit 3e64b43
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 10 deletions.
14 changes: 14 additions & 0 deletions demos/er.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,20 @@
number final_price
}
</pre>
<hr />

<pre class="mermaid">
erDiagram
"HOSPITAL" {
int id PK
int doctor_id FK
string address UK
string name
string phone_number
string fax_number
}
</pre>
<hr />

<script src="./mermaid.js"></script>
<script type="module">
Expand Down
6 changes: 3 additions & 3 deletions docs/syntax/entityRelationshipDiagram.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,14 +234,14 @@ The `type` and `name` values must begin with an alphabetic character and may con

#### Attribute Keys and Comments

Attributes may also have a `key` or comment defined. Keys can be "PK" or "FK", for Primary Key or Foreign Key. And a `comment` is defined by double quotes at the end of an attribute. Comments themselves cannot have double-quote characters in them.
Attributes may also have a `key` or comment defined. Keys can be "PK", "FK" or "UK", for Primary Key, Foreign Key or Unique Key. And a `comment` is defined by double quotes at the end of an attribute. Comments themselves cannot have double-quote characters in them.

```mermaid-example
erDiagram
CAR ||--o{ NAMED-DRIVER : allows
CAR {
string allowedDriver FK "The license of the allowed driver"
string registrationNumber
string registrationNumber UK
string make
string model
string[] parts
Expand All @@ -261,7 +261,7 @@ erDiagram
CAR ||--o{ NAMED-DRIVER : allows
CAR {
string allowedDriver FK "The license of the allowed driver"
string registrationNumber
string registrationNumber UK
string make
string model
string[] parts
Expand Down
2 changes: 1 addition & 1 deletion packages/mermaid/src/diagrams/er/parser/erDiagram.jison
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ accDescr\s*"{"\s* { this.begin("acc_descr_multili
"erDiagram" return 'ER_DIAGRAM';
"{" { this.begin("block"); return 'BLOCK_START'; }
<block>\s+ /* skip whitespace in block */
<block>\b((?:PK)|(?:FK))\b return 'ATTRIBUTE_KEY'
<block>\b((?:PK)|(?:FK)|(?:UK))\b return 'ATTRIBUTE_KEY'
<block>(.*?)[~](.*?)*[~] return 'ATTRIBUTE_WORD';
<block>[A-Za-z][A-Za-z0-9\-_\[\]\(\)]* return 'ATTRIBUTE_WORD'
<block>\"[^"]*\" return 'COMMENT';
Expand Down
9 changes: 5 additions & 4 deletions packages/mermaid/src/diagrams/er/parser/erDiagram.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,17 +176,18 @@ describe('when parsing ER diagram it...', function () {
expect(entities[entity].attributes.length).toBe(1);
});

it('should allow an entity with attribute starting with fk or pk and a comment', function () {
it('should allow an entity with attribute starting with fk, pk or uk and a comment', function () {
const entity = 'BOOK';
const attribute1 = 'int fk_title FK';
const attribute2 = 'string pk_author PK';
const attribute3 = 'float pk_price PK "comment"';
const attribute3 = 'string uk_address UK';
const attribute4 = 'float pk_price PK "comment"';

erDiagram.parser.parse(
`erDiagram\n${entity} {\n${attribute1} \n\n${attribute2}\n${attribute3}\n}`
`erDiagram\n${entity} {\n${attribute1} \n\n${attribute2}\n${attribute3}\n${attribute4}\n}`
);
const entities = erDb.getEntities();
expect(entities[entity].attributes.length).toBe(3);
expect(entities[entity].attributes.length).toBe(4);
});

it('should allow an entity with attribute that has a generic type', function () {
Expand Down
4 changes: 2 additions & 2 deletions packages/mermaid/src/docs/syntax/entityRelationshipDiagram.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,14 @@ The `type` and `name` values must begin with an alphabetic character and may con

#### Attribute Keys and Comments

Attributes may also have a `key` or comment defined. Keys can be "PK" or "FK", for Primary Key or Foreign Key. And a `comment` is defined by double quotes at the end of an attribute. Comments themselves cannot have double-quote characters in them.
Attributes may also have a `key` or comment defined. Keys can be "PK", "FK" or "UK", for Primary Key, Foreign Key or Unique Key. And a `comment` is defined by double quotes at the end of an attribute. Comments themselves cannot have double-quote characters in them.

```mermaid-example
erDiagram
CAR ||--o{ NAMED-DRIVER : allows
CAR {
string allowedDriver FK "The license of the allowed driver"
string registrationNumber
string registrationNumber UK
string make
string model
string[] parts
Expand Down

0 comments on commit 3e64b43

Please sign in to comment.