It's modeling time
Tags: hasura, datamodeling
Published: 2017-06-29 13:19:24

---
title: It's modeling time
published: true
description: data modeling of my app Snipcode.
tags: hasura, data-modeling
cover_image: https://thepracticaldev.s3.amazonaws.com/i/11ctl9v16ndnh3r5l877.png
---

After all setup, we are in the stage of data modeling where I have to define the schema of the data I am going to use in my web app.

## Features

- Code Pasting using Code mirror
- Making snippets private
- Adding search page with a random snippet option

Upcoming features include starring someone's code snippet and adding a count of the number of views of that snippet.

These many features will require me to add several tables:-

- One table to store users data
- One for tags
- And the last one for codes

## Modeling

One basic rule you must follow is-

> Never put same string data twice. Use relationship instead.

This means if you are using this table named 'cities_info'

| id  | cities     | State     |
|:---:|:---------: |:-----:    |
| 1   | Chennai    |Tamil Nadu |
| 2   | Kanchipuram|Tamil Nadu |
| 3   | Kolkata    |West Bengal|

here 'Tamil Nadu' is coming twice which is not good for our database. Thus there should be another table storing all the states

|id |states|
|:-:|:----|
|1  |Tamil Nadu|
|2  |West Bengal|
|3  |Gujrat|

and then a foreign key will be added in the table 'cities_info' instead of a State column. So the resulting table 'cities_info' will be


| id  | cities     |state_id|
|:---:|:---------: |:-----:|
| 1   | Chennai    |1      |
| 2   | Kanchipuram|1      |
| 3   | Kolkata    |2      |

In this integers are repeating but that's not the problem. The problem is with repeating strings.

In my case, the tables and their relationship are as follows:-

![](https://thepracticaldev.s3.amazonaws.com/i/npk4szuxdvcs8hx82vyc.png)

I designed it using [dbdesigner tool](http://dbdesigner.net/). It can be done by any other means also such as [pony orm](https://ponyorm.com/). 

These tables satisfied all the functionalities of my web app like storing code snippets in the code table, the language in language table and tags in the tag table.

One interesting table is tag_code which is a perfect example of the basic rule I discussed earlier.

Next post, is going to be about hasura data API and postman collection for querying them.


*Here is the index of all the post regarding this series of snipcode developemnt*

[Part I: App Idea](https://dev.to/sroy8091/app-idea-for-hasura-internship)
[Part II: App prototype](https://dev.to/sroy8091/app-prototype)
[Part III: Local Development](https://dev.to/sroy8091/hasura-local-development)
[Part IV: G for Git](https://dev.to/sroy8091/g-for-git)
[Part V: Data Modeling](https://dev.to/sroy8091/its-modeling-time)
[Part VI: Data & Auth APIs](https://dev.to/sroy8091/postman-a-major-tool-for-developers)
[Part VII: Basic Functionalities](https://dev.to/sroy8091/i-love-cookies-expressjscookies)
[Part VIII: App Screen 1](https://dev.to/sroy8091/i-built-it-with-codemirror)
[Part IX: App Screen 2](https://dev.to/sroy8091/app-screen-2)
[Part X: App Screen 3](https://dev.to/sroy8091/search-page-using-postgres-full-text-search-feature)
[Part XI: User Reviews](https://dev.to/sroy8091/improvements-in-my-web-app)
[Part X: Final Submission](https://dev.to/sroy8091/final-app-submission)