Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9235325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:00:07+00:00 2026-06-18T07:00:07+00:00

I use SQLite3 and MySQL on the same Rails project but on two different

  • 0

I use SQLite3 and MySQL on the same Rails project but on two different computers. I noticed that the schema.rb which is generated when I run all migrations looks different for both environments. When I run the migrations in the SQLite3 environment the following statements are removed from the schema.rb file.

add_index "places", ["user_id"], :name => "places_user_id_fk"
add_foreign_key "places", "users", :name => "places_user_id_fk"

Please note, that I use the foreigner gem which extends migrations with add_foreign_key and remove_foreign_key.

Here are the migrations and model relevant to the problem:

# 20130123004056_create_places.rb
class CreatePlaces < ActiveRecord::Migration
  def change
    create_table :places do |t|
      t.string :name
      t.string :location
      t.integer :user_id
      t.timestamps
    end
  end
end

…

# 20130123234250_add_foreign_key.rb
class AddForeignKey < ActiveRecord::Migration
  def change
    add_foreign_key(:places, :users)
  end
end

…

# user.rb
class User < ActiveRecord::Base
  has_many :places
end

…

# place.rb
class Place < ActiveRecord::Base
  belongs_to :user    
end

Question: How can I define the relationship between users and places in a way both SQLite3 and MySQL can handle it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T07:00:09+00:00Added an answer on June 18, 2026 at 7:00 am

    The foreigner README clearly states

    The following adapters are supported:

    • sqlite (foreign key methods are a no-op)

    So your SQLite database does not have foreign key constraints set up because foreigner doesn’t support them. When db/schema.rb is generated from the SQLite database, this is why there are no foreign keys specified.

    The Rails Guide on Migrations mentions foreign keys quite a bit

    If you need to perform tasks specific to your database (for example create a foreign key constraint) then the execute method allows you to execute arbitrary SQL

    There is even an example of how to add/remove a foreign key.

    Having used foreigner when first starting out with Rails, I suggest you drop it from your Gemfile and either

    1. Don’t use foreign keys at all; just specify your cascades on your ActiveRecord associations
    2. Use the execute migration method as described in the linked example above (and just make sure all the different RDBMS’ support whatever you put into the execute methods)
    3. Stop using SQLite in development if you insist on using foreign keys. You’re not going to use SQLite in production anyway, and are already using better RDMS (MySQL, “better” in the sense that it has the foreign key support you are looking for) on other machines.

    As you pointed out in the comments, SQLite has lacking support for adding foreign keys after the table has been created; they cannot be added through a futre migration in Rails. I personally suggest you use choice 1 or 3, as it is going to be more difficult to create a solution through execute commands in migrations that satisfies the restrictions of SQLite while having the same end result on other RDMS’.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use sqlite3 in my Rails 3 development environment instead of MySQL
I am trying to use sqlite3 in cygwin and I notice that the backspace
I want to use sqlite3 for my iphone application. But it is not working.
I have an app that needs to be able use either an sqlite3 datebase
What is a good .gitignore to use with Rails on Heroku? *.log *.sqlite3 what
Locally I use sqllite3 as a database, but in production I use MySQL. When
In my django project, I use mysql db for production, and sqlite for tests.
I am learning how to use sqlite3 in python. I have a simple table
I'm writing a very simple and small wrapper around sqlite3, and use sqlite3_get_table() to
I use bash to access a sqlite3 database. Is there a way to get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.