4
こんにちは、私は移行に取り組んでおり、各テーブルのデフォルト'id'
を削除します。私は'student_id'
代わりに特別なフィールドを作成し、私はそれがここに1001Ruby on Rails 5でカスタムIDを自動的に開始値でインクリメントする方法は?
で始まる自動インクリメントが私のコードだようにしたい:
class CreateStudents < ActiveRecord::Migration[5.0]
def up
create_table :students, :id => false do |t|
t.integer "student_id"
t.string "first_name", :limit => 25
t.string "last_name", :limit => 50
t.string "email", :default => ' ', :null => false
t.string "birthday"
t.string "subjects"
t.string "teachers"
t.string "username", :limit => 25
t.string "password_digest", :limit => 40
t.timestamps
end
execute "CREATE SEQUENCE students_student_id_seq OWNED BY students.student_id INCREMENT BY 1 START WITH 1001"
end
def down
drop_table :students
execute "DELETE SEQUENCE students_student_id_seq"
end
end
私はFFのエラーを得た:
Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SEQUENCE students_student_id_seq OWNED BY students.student_id INCREMENT BY 1 STA' at line 1
どのように自動にRuby on Rails 5でカスタム値をインクリメントするには?
これはすべてですか?またはstudent_idフィールドを特定のものにする必要がありますか?私はそれをマイグレーションした後にレコードを追加しようとしましたが、ロールバックしています。 –
私はそのテーブルのPKを設定する必要があると思います。 –
あなたはそのフィールドへのインデックスを意味しますか?このようにadd_index( "students"、 "student_id")??? –