sql

All things SQL and JDBC...
jaime 2020-03-27T09:38:07.020300Z

Hi, what is your dev workflow when working with database migrations? 1. What tool do you use? 2. Are you using repl to generate migration scripts, as well as when running them? 3. Do you seed data? How? 4. How do you do it in production?

💯 1
âž• 1
dharrigan 2020-03-27T10:04:33.020700Z

flywaydb

2020-03-27T10:40:10.022500Z

@jaime.sangcap we have beein using migratus for a quiet a while. https://github.com/yogthos/migratus no complains at all We normally run migrations when the app starts in prod. however not when started via the repl.

2020-03-27T10:41:33.023500Z

seed data normally is also part of the migrations for us.

jaime 2020-03-27T12:06:34.024600Z

thanks for sharing. I like the idea of using the flyway api instead of command due to file size IMO.

jaime 2020-03-27T12:09:45.024800Z

cool. I'm also looking into migratus. do you have the seed data in dev folder?

jaime 2020-03-27T12:11:43.025Z

I'm thinking of creating migration.clj file and expose -main function for cli, since I'm using deps.edn. I Need to read parse-opts though

2020-03-27T18:27:26.025300Z

we have seed data together with migrations, so on resoruces/migrations

danielglauser 2020-03-27T22:11:28.028700Z

Trying to insert a record with org.clojure/java.jdbc into Postgres and it keeps complaining that I’m not specifying an id.

Execution error (PSQLException) at org.postgresql.core.v3.SimpleParameterList/checkAllParametersSet (SimpleParameterList.java:257).
No value specified for parameter 1.
Tried:
(jdbc/execute! db ["INSERT INTO CLASS_SONGS (class_id,track_number,song_id,start_time) VALUES (?, ?, ?, ?)"]
                 {:class_id (:id class)                                         
                  :track_number track                                           
                  :song_id (:id song)                                           
                  :start_time start-time})
And:
(jdbc/insert! db :class_songs {:class_id (:id class)                                         
                  :track_number track                                           
                  :song_id (:id song)                                           
                  :start_time start-time})
The table looks like:
Column    |  Type   | Collation | Nullable |                 Default
--------------+---------+-----------+----------+-----------------------------------------
 id           | integer |           | not null | nextval('class_songs_id_seq'::regclass)
 class_id     | integer |           | not null |
 track_number | integer |           |          |
 song_id      | integer |           | not null |
 start_time   | integer |           |          |
I want Postgres to just trigger the nextval function when I don’t specify and id. This works in other languages. Any ideas?

2020-03-27T22:17:36.030100Z

do you have the error from the insert! call? the error you posted looks like it is from the execute! call, and that error is because you are not passing in the values for the parameterized query correctly

2020-03-27T22:18:43.031200Z

for execute! like that you can't pass in a map like that (it is treating that map as the options map) you need to pass in each value matching a '?' in the vector, similar to with query

danielglauser 2020-03-27T22:23:49.031600Z

Execution error (PSQLException) at org.postgresql.core.v3.QueryExecutorImpl/receiveErrorResponse (QueryExecutorImpl.java:2477).
ERROR: null value in column "id" violates not-null constraint
  Detail: Failing row contains (null, 8487, 1, null, 0).

2020-03-27T22:27:35.032500Z

are you sure your insert! is as shown?

2020-03-27T22:28:42.033900Z

you have 5 columns and five values in the row there, but insert! doesn't actually know how many a columns a table it is inserting to has (it doesn't do any reading of the table metadata, etc) so it wouldn't know to insert 5 values unless you gave it five

danielglauser 2020-03-27T22:28:43.034100Z

Yes, just double checked.

danielglauser 2020-03-27T22:30:06.035300Z

Interesting. In Go folks on the team aren’t specifying anything for id and Postgres happily runs the nextval function.

2020-03-27T22:30:21.035700Z

right, and I think somehow you are specifying an id

2020-03-27T22:30:40.036Z

is it a literal map that is being inserted?

danielglauser 2020-03-27T22:32:27.037Z

🤦

danielglauser 2020-03-27T22:32:45.037500Z

It’s the song that’s missing the id.

danielglauser 2020-03-27T22:33:32.038200Z

So the error is weird, it should complain about song_id being null.

danielglauser 2020-03-27T22:33:49.038600Z

Once I generated the map literal it was obvious.

danielglauser 2020-03-27T22:34:03.039200Z

Thanks for the help Kevin! 🙂

2020-03-27T22:34:49.040100Z

do you have fk constraints or triggers or something, it could be reporting the error based on the song table (where presumably the column is named id)

2020-03-27T22:34:51.040300Z

🙂

danielglauser 2020-03-27T22:35:33.040600Z

Foreign-key constraints:
    "class_songs_class_id_fkey" FOREIGN KEY (class_id) REFERENCES classes(id)
    "class_songs_song_id_fkey" FOREIGN KEY (song_id) REFERENCES songs(id)

danielglauser 2020-03-27T22:35:36.040800Z

Yup, that’s it.