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 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

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 2357
Alex Hales
  • 0
Alex HalesTeacher
Asked: May 31, 20222022-05-31T22:54:39+00:00 2022-05-31T22:54:39+00:00

mysql – Unable to Insert row in an existing table via migrations – Rails 6

  • 0

[ad_1]

I have a table with about 10 values which are used to populate some view in the application. Table is static as in no operation in the app exists to add/modify data in it. I was tasked with adding a row to this table and was exploring how I could do it. (Do not have write access to the table directly so cannot run queries on the sql server).

I was thinking of creating a migration and using Model.create or Model.new command to create the row. Something like:

class AddNewRowToTestTable < ActiveRecord::Migration[6.1]
  def change
    Model_Name.create({:type_id => "a123", :name => "Test Value", :program => "Test Program"})
  end
end

The above would work and it adds the correct timestamps (for created_at and updated_at columns). However, I came across this stackoverflow post: How do I add some inserts in rails migration? where it is stressed that using a model for data modification is a bad practice as models might change over time and to use raw sql instead. So I tried doing this: (Had to add timestamp code as otherwise it would create null values)

class AddNewRowToTestTable < ActiveRecord::Migration[6.1]
  def change
    execute "insert into table_name (type_id, name, program, created_at, updated_at) values ('a123', 'Test Value', 'Test Program', '#{Time.now}', '#{Time.now}')"
  end
end

However, I get the following error when running the migration:

Strong Migrations does not support inspecting what happens inside an
execute call, so cannot help you here. Please make really sure that what
you’re doing is safe before proceeding, then wrap it in a safety_assured { … } block.

The query works when I run the sql directly in a test db. The created_at and updated_at columns show up null. (Was not sure how to add rails specific timestamps)

insert into table_name (type_id, name, program) values ('a123', 'Test Value', 'Test Program');

Total rails noob here and would appreciate advice on what I might be doing wrong or the right way of doing this.

Use Rails 6 and Ruby 2.7. Do let me know if any additional details are required. Thanks.

P.S: I do not see a seeds.rb file in our codebase, so not sure how to go about that.

[ad_2]

  • 0 0 Answers
  • 8 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 3 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 5 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 4 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

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.