0

I'm working on a three part program for a webcrawler program for school. My first step is to just create the database using activerecord and knowing that it will be queried later using sqlite3. The database actually does get created with my current program. However, it errors out when gets to "add_index". I'm not great with databases in general so guidance is appreciated.

Before I put in my code the error is:

crawler_create.rb:23: syntax error, unexpected ',', expecting ')'
add_index (:pages,[:url], unique=> true)

Here's what I've come up with so far:

require 'active_record'
require 'sqlite3'

db=SQLite3::Database.new("crawler.db")

ActiveRecord::Base.establish_connection(adapter:'sqlite3',database:'crawler.db')

ActiveRecord::Schema.define do
  create_table :pages do |t|
    t.string :url, :null => false
    t.string :title
    t.string :content_type
    t.date :last_modified
    t.string :status
  end
end

add_index (:pages,[:url], unique=> true)

ActiveRecord::Schema.define do
  create_table :links do |t|
    t.integer :from_page_id, :null => false
    t.integer :to_page_id, :null => false
  end
end

add_index(:links,[:from_page_id,:pages_id], :unique =>true)

ActiveRecord::Schema.define do
  add_foreign_key :from_page_id, :pages_id
  add_foreign_key :to_page_id,:pages_id
end
  • 1
    You are so so close `add_index` should be inside the `Schema.define` block just below the `create_table` block. Also here `add_index (:pages,[:url], unique=> true)` you just forgot to make `unique` a symbol e.g. `:unique => true` or `unique: true`. Make these changes where appropriate and you should be good to go – engineersmnky Dec 01 '17 at 16:07

1 Answers1

0

It looks like you got a simple syntax error here:

add_index (:pages, [:url], unique: true)

I'd suggest that you use a consistent hash syntax, either using hash rockets => or the newer syntax unique: true.

23tux
  • 14,104
  • 15
  • 88
  • 187