0

Logistic & Rental Requests belong to each other via a TypeLogistic table. I accidentally created a bad TypeLogistic, so now there are two:

TypeLogistic.where(rental_request_id:200).where(logistic_id:130)
=> #<ActiveRecord::Relation [#<TypeLogistic rental_request_id: 200, logistic_id: 130, type_of_logistics: nil>, #<TypeLogistic rental_request_id: 200, logistic_id: 130, type_of_logistics: "delivery">]>

How do I destroy the first one? Tried running below, didn't work

TypeLogistic.where(rental_request_id:200).where(logistic_id:130).first.destroy
PG::Error: ERROR:  zero-length delimited identifier at or near """"
LINE 1: DELETE FROM "type_logistics" WHERE "type_logistics"."" = $1
                                                            ^
james
  • 3,989
  • 8
  • 47
  • 102

1 Answers1

2

The error is being thrown because the TypeLogistic join table does not have a primary key. Incidentally, this is the default setting it appears, since I followed the instructions to create the join table without altering anything.

Detailed info here: Rails Devise PG::SyntaxError: ERROR: zero-length delimited identifier at or near """"

Solution? Add a primary key, see this answer below:

Add Id column in a migration

Community
  • 1
  • 1
james
  • 3,989
  • 8
  • 47
  • 102
  • Worth noting that this can also happen if you add to a relation for a join table, which I just ran into. e.g. if you have Book, Author, and a joining model called BookAuthor, on Author you'd have `has_many :book_authors` and then a has_many :through for the relationship to the books. If you have `has_many :book_authors, dependent: :destroy`, that's wrong and the dependent destroy shouldn't be on the has_many that associates to the joining model. – Connor Shea Jun 15 '21 at 22:01