In migrations, it's good to avoid referencing external app code (such as the existence of a Link ActiveRecord model).  Instead, define the model locally, inside of the migration class, like

```ruby
class AddEventIdAtCreationToLinks < ActiveRecord::Migration
  class Link < ActiveRecord::Base; end
  class Event < ActiveRecord::Base; end

  def up
    ...
```

Also, it's better to use `all.find_each` instead of `all.each` because `all` will load all Links in the database into memory, while `find_each` will find in batches of (by default) 1000 records at a time.