- Set up database connection using knex in a brand new express project
- Create migrations
- Create seeds
- Create empty npm project
- Install
knexandpg - Create
knexfile.jsand add configuration. Where would you get that configuration from? - Create
dbfolder - Create
index.jsfile. What is contained in this file? Where would you find an example of it? - Add
knexscript topackage.json - Create database
- Create a migrations for school-api. What is the command to create a migration
| instructors |
|---|
| id serial |
| name varchar(255) not nullable |
| timestamps |
| cohorts |
|---|
| id serial |
| name varchar(255) not nullable |
| timestamps |
| students |
|---|
| id serial |
| name varchar(255) not nullable |
| cohort_id integer references cohorts.id not nullable |
| timestamps |
| instructors_cohorts |
|---|
| id serial |
| instructors_id integer references instructors.id not nullable |
| cohorts_id integer reference cohorts.id not nullable |
| timestamps |
- What is the command to run a migration?
- Would a migration run more than once?
- How do you rollback migrations?
- In what order do migrations run?
- Create seeds for
school-api. What is the command to create a seed file
| id | name |
|---|---|
| 1 | Ada Lovelace |
| 2 | Alan Turing |
| 3 | Grace Hopper |
| 4 | Jon Von Neumann |
| id | name |
|---|---|
| 1 | Tiger Sharks |
| 2 | Velociraptors |
| 3 | Hawks |
| 4 | Lemurs |
| id | name | cohort_id |
|---|---|---|
| 1 | Abe | 2 |
| 2 | Bryan | 2 |
| 3 | Claudia | 1 |
| 4 | Daniel | 3 |
| 5 | Dustin | 4 |
| 6 | Gavin | 1 |
| 7 | Luda | 4 |
| 8 | Mark | 4 |
| 9 | Sunil | 3 |
| 10 | Vika | 3 |
| id | instructors_id | cohorts_id |
|---|---|---|
| 1 | 1 | 4 |
| 2 | 2 | 1 |
| 3 | 3 | 3 |
| 4 | 4 | 2 |
- Reset the sequence for each table
.then(() => {
// reset sequence
return knex.raw(`SELECT setval('${TABLE_NAME}_id_seq', (SELECT MAX(id) FROM ${TABLE_NAME}));`)
})
- What happens when you run seeds more than once?
- How can you fix constraint errors?
- Add on cascade delete
- Create a seed file to delete in the right order
- How do you roll back seeds?
- In what order do seeds run? Does it matter?
- Where do the migrations folder and seed folder get configured?