I'm getting a weird error. I had sqlite3 working and now switching over to postgres, i'm getting this error.
I have migrations, and I had to add a new column.
create_ideas migration
class CreateIdeas < ActiveRecord::Migration
def change
create_table :ideas do |t|
t.string :name
t.text :description
t.string :picture
t.timestamps
end
add_foreign_key :ideas, :lists
end
end
add_list_id_column_to_ideas migration
class AddListIdColumnToIdeas < ActiveRecord::Migration
def change
add_column :ideas, :list_id, :integer
end
end
What is wrong with my foreign key? I get the error when I do rake db:migrate
I have another migration with foreign key, and no errors yet? Maybe its catching the first error first before seeing the other one, but I have this one too:
create_comments migration
class CreateComments < ActiveRecord::Migration
def change
create_table :comments do |t|
t.string :user_name
t.text :body
t.integer :idea_id
t.timestamps
end
add_foreign_key :comments, :ideas
end
end
last migration I have create_lists
class CreateLists < ActiveRecord::Migration
def change
create_table :lists do |t|
t.string :name
t.text :description
t.string :picture
t.timestamps
end
end
end
Any clue why I'm getting errors?
Thanks
edit
my relationships
comments model
belongs_to :idea
idea model
belongs_to :list
has_many :comments
list model
has_many :ideas
Related
Is it possible to state that a belongs_to type of field can't be null? Right now I have the following migration:
class CreatePosts < ActiveRecord::Migration[6.0]
def change
create_table :posts do |t|
t.belongs_to :site
t.string :title
t.timestamps
end
end
end
The generates a table where site_id can be null.
you can add options to belongs_to, so here you can add null:false option.
class CreatePosts < ActiveRecord::Migration[6.0]
def change
create_table :posts do |t|
t.belongs_to :site, null: false
t.string :title
t.timestamps
end
end
end
I find this issue when I try to save the a team, how can I solve it? I am trying to deal with these associations for so long, if you guys do other issues, just let me know, please (this association has been a nightmare).
Here are the models
class Field < ApplicationRecord
end
class Game < ApplicationRecord
belongs_to :field
belongs_to :organiser
has_one :team
has_many :players, through: :team
end
class Organiser < ApplicationRecord
has_many :games
end
class Player < ApplicationRecord
has_many :teams
has_many :games, through: :teams
end
class Team < ApplicationRecord
has_many :players
belongs_to :game
end
Here are the migrations
class CreateOrganisers < ActiveRecord::Migration[5.2]
def change
create_table :organisers do |t|
t.string :name
t.string :email
t.integer :age
t.timestamps
end
end
end
class CreatePlayers < ActiveRecord::Migration[5.2]
def change
create_table :players do |t|
t.string :name
t.integer :age
t.string :address
t.timestamps
end
end
end
class CreatePlayers < ActiveRecord::Migration[5.2]
def change
create_table :players do |t|
t.string :name
t.integer :age
t.string :address
t.timestamps
end
end
end
class CreateFields < ActiveRecord::Migration[5.2]
def change
create_table :fields do |t|
t.string :location
t.string :transports
t.timestamps
end
end
end
class CreateGames < ActiveRecord::Migration[5.2]
def change
create_table :games do |t|
t.references :field, foreign_key: true
t.references :organiser, foreign_key: true
t.integer :size
t.timestamps
end
end
end
class CreateTeams < ActiveRecord::Migration[5.2]
def change
create_table :teams do |t|
t.references :player, foreign_key: true
t.references :game, foreign_key: true
t.timestamps
end
end
end
class AddTeamToGames < ActiveRecord::Migration[5.2]
def change
add_column :games, :team, :reference
end
end
The idea is to make sure that each game will have a team of certain people. I want to access the people through game.team.player
Your migrations doesn't have any kind of references to connect these tables.
For example Game has_one :team is not going to create this references for you, to call game.team needs a column called game_id in teams table.
To add the reference you can create a migration for it:
rails g migration AddGameToTeams game:references
This will create that migration file may look like this:
class AddGameToTeams < ActiveRecord::Migration
def change
add_reference :teams, :game, index: true
end
end
Depending in the version you running rails this migration file may differ a little with adding an extra line regarding foreign_key.
Run the generated migration with rails db:migrate and it should work.
You need to apply the same concept to the rest of your tables.
Hope this helps.
I have a Project migration class like this:
class CreateProjects < ActiveRecord::Migration
def change
create_table :projects do |t|
t.string :title
t.text :description
t.boolean :public
t.references :user, index: true, foreign_key: true
t.timestamps null: false
end
end
end
It creates a column name user_id in projects table but I want to name the column owner_id so I can use project.owner instead of project.user.
You can do it two ways:
#app/models/project.rb
class Project < ActiveRecord::Base
belongs_to :owner, class_name: "User", foreign_key: :user_id
end
OR
$ rails g migration ChangeForeignKeyForProjects
# db/migrate/change_foreign_key_for_projects.rb
class ChangeForeignKeyForProjects < ActiveRecord::Migration
def change
rename_column :projects, :user_id, :owner_id
end
end
then:
$ rake db:migrate
I'm working on a twitter clone and have been having trouble with some of my associations. I want a user to have many tweets. My tweets will have a many-to-many relationship with hashtags, so in theory I want to be able to call User.find(1).hashtags and have a list of the user's hashtags (edit: solved this issue). I also am having trouble pushing hashtags into a specific tweet.
Here are my models:
class User < ActiveRecord::Base
has_many :tweets
end
class Tweet < ActiveRecord::Base
belongs_to :user
has_and_belongs_to_many :hashtags
end
class Hashtag < ActiveRecord::Base
has_and_belongs_to_many :tweets
end
Here are my migrations:
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :username
t.string :email
t.string :password
t.timestamps
end
end
end
class CreateTweets < ActiveRecord::Migration
def change
create_table :tweets do |t|
t.string :content
t.belongs_to :user
t.timestamps
end
end
end
class CreateHashtags < ActiveRecord::Migration
def change
create_table :hashtags do |t|
t.string :tag
t.timestamps
end
end
end
class CreateHashtagsTweets < ActiveRecord::Migration
def change
create_table :hashtags_tweets, id: false do |t|
t.references :hashtag
t.references :tweet
end
end
end
Any help would be greatly appreciated! Thanks!
Is it normal that rails doesn't create the foreign keys on database? Or I'm doing something wrong?
I have these models:
class City < ActiveRecord::Base
has_many :users
end
class User < ActiveRecord::Base
belongs_to :city
end
and their respective migrations:
class CreateCities < ActiveRecord::Migration
def change
create_table :cities do |t|
t.string :name
t.timestamps
end
end
end
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name
t.references :city, index: true
t.timestamps
end
end
end
That's correct. Rails doesn't automatically add foreign keys, you have to specify it yourself in the migration like you have.
t.references :city is effectively the same as t.integer :city_id.
Are you saying that even though are specifying a foreign key, that you aren't seeing the results?