Sqlite & rails: change primary key column?

I have a table in my database (sqlite) called books, which was automatically set by the id rails 3 column when the table was created. The table also has a column called "isbn", which is obviously an integer.

Now, I would like to change the primary key of the tables as an isbn column, and then delete the original identity column.

Can this be done with rail migrations? How?

+3
source share
2 answers

Most likely, but do not do this.

If you want to use isbn for the query, upgrade your models as follows instead

class Book < ActiveRecord::Base

  def to_param
    self.isbn
  end

end

and your controller

class BooksController

  def show
    @book = Book.find_by_isbn(params[:id])
  end

  # and similar for other actions
end
+2
source

ISBN - , , . ISBN - "" /.

, , ISBN. SQLite . Rails.

+2

Source: https://habr.com/ru/post/1792806/


All Articles