I'm having trouble with being able to create a friendship (which I've named a 'connection') in the rails console, so that users can be friends (which I've named 'contacts'):
2.7.0 :033 > user4.contact_request(user5)
Connection Exists? (0.3ms) SELECT 1 AS one FROM "connections" WHERE "connections"."user_id" = ? AND "connections"."contact_id" = ? LIMIT ? [["user_id", 7], ["contact_id", 8], ["LIMIT", 1]]
TRANSACTION (0.1ms) begin transaction
Connection Create (1.1ms) INSERT INTO "connections" ("user_id", "contact_id", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["user_id", 7], ["contact_id", 8], ["created_at", "2021-04-19 23:45:32.553041"], ["updated_at", "2021-04-19 23:45:32.553041"]]
TRANSACTION (0.1ms) rollback transaction
Traceback (most recent call last):
4: from (irb):32
3: from (irb):33:in `rescue in irb_binding'
2: from app/models/user.rb:49:in `contact_request'
1: from app/models/user.rb:50:in `block in contact_request'
ActiveRecord::StatementInvalid (SQLite3::SQLException: no such table: main.contacts)
In the episode, I was using mysql, so I don't think that's the issue. Are you by chance using Spring? Maybe bin/spring stop and restart your application to see if that helps.
class CreateConnections < ActiveRecord::Migration[6.1]
def change
create_table :connections do |t|
t.references :user, null: false, foreign_key: { to_table: :users}
t.references :contact, null: false, foreign_key: {to_table: :users}
t.integer :status, default: 0, limit: 1
t.timestamps
end
end
end
I tried to edit the migration as above based on comments made by marketing. I just directly edited the file, I wasn't sure if I needed to run a new migration. From what I've read on stack overflow and other places, the error I'm getting seems to be something to do with the migrations.
☒ if you've not deployed the application yet then you can roll back the database migrations first and then make the changes. If you have already deployed the application then things get a bit trickier because you've already gotten data populated on those columns. If you've already deployed the application or have team mates and the code has already been merged then I would create another migration.
☒But are the migration changes necessary from what you can see? Or do I need to send you through some more code? The project is deployed on GitHub but it's only a dummy app to test code before making larger, nice changes in the real repository (I know it seems unnecessary but it helps give a lot more room for error).
I could have a look this week. I'm not sure if there could be something that's changed with the Rails version or of there is some other hiccup that could be going on.
Hi ☒ , just wondering if you had a chance to take a look at it this week? I'm still stuck! I made a stack overflow post that has had no traction yet: https://stackoverflow.com/questions/67171189/activerecordstatementinvalid-sqlite3sqlexception-no-such-table-main-conta
I'll have a bit more of a sift through all the code that you have in the other tab here to see if there are any other major discrepancies.
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::UndefinedTable: ERROR: relation "friends" does not exist
/Users/luke/Desktop/Actualize/soundjek-v-2/soundjek-backend/db/migrate/20200827013143_create_friendships.rb:3:in `change'
bin/rails:4:in `<main>'
Caused by:
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "friends" does not exist
/Users/luke/Desktop/Actualize/soundjek-v-2/soundjek-backend/db/migrate/20200827013143_create_friendships.rb:3:in `change'
bin/rails:4:in `<main>'
Caused by:
PG::UndefinedTable: ERROR: relation "friends" does not exist
/Users/luke/Desktop/Actualize/soundjek-v-2/soundjek-backend/db/migrate/20200827013143_create_friendships.rb:3:in `change'
bin/rails:4:in `<main>'
Tasks: TOP => db:migrate
I'm not sure what to do here!
User model:
and the Connection model:
I've tried double and triple-checking my code to make sure it's the same as yours but I can't quite figure it out!
I downloaded the source and got it running and it still seems to work okay.
You do have two "has_many :connections" defined in the user model which is a bit odd.
Odd that you're able to get it working... would it make any difference that I've created my app with PostgresQL and not SQLite?
and the connections migration, if it helps:
I tried to edit the migration as above based on comments made by marketing. I just directly edited the file, I wasn't sure if I needed to run a new migration. From what I've read on stack overflow and other places, the error I'm getting seems to be something to do with the migrations.
https://stackoverflow.com/questions/67171189/activerecordstatementinvalid-sqlite3sqlexception-no-such-table-main-conta
I'll have a bit more of a sift through all the code that you have in the other tab here to see if there are any other major discrepancies.