[Rails/Postgres] DB migrate scripts break on references


(Joe Tseng) #1

I've been following online examples of how to create tables the Rails way
and I noticed the migration scripts I created containing references don't
work out of the box. I thought maybe I did something wrong so I tried just
now making a test application/migration against a *SQLite3* datasource. I
ran the following commands:

$ rails g scaffold SchoolClass name
$ rails g scaffold Student name SchoolClass:references

And it made my migration scripts (along with everything else).
<img src=’/uploads/default/original/2X/1/1d600dbb8c34ca43e53061bf69b32f46734ea8c8.png’ width=‘434’ height=‘190’>

I ran the migration scripts and it worked fine.
<img src=’/uploads/default/original/2X/3/30bdd3960220c5860f6110934876781407f8a124.png’ width=‘400’ height=‘187’>

When I tried doing the same steps above against a *Postgres* datasource, I
kept on getting the following:

PG::UndefinedTable: ERROR: relation does not exist

The only way I got past this issue is if I went into the migration script
and updated the t.references line and replaced the class name with the
actual table name. It's a simple enough fix, but I've got a ton of tables
to migrate and each table can have a good number of references. Has anyone
seen this before, and is there some way to keep on using the scaffold
utility to make migrations?

Appreciative of any useful assistance,

- Joe

···

--

If you type "Google" into Google, you can break the Internet. -- Jen Barber


(botp) #2

try using snake_case notation. eg,
bin/rails g scaffold school_class name
bin/rails g scaffold student name school_class:references

best regards --botp

···

On Wed, Oct 10, 2018 at 7:41 AM Joe Tseng <jtsengorg@gmail.com> wrote:

$ rails g scaffold SchoolClass name
$ rails g scaffold Student name SchoolClass:references


(Andy Jones) #3

1) You should be able to use either CamelCase or under_scored in the “scaffold name” part of `rails generate scaffold NAME …` .

2) Your problem comes from the fact you are using CamelCase to refer to the already existing table school_class. You can’t do that there, only in the scaffold name part when you first create it.

3) Just so you know: this isn’t a Rails mailing list.

···

From: ruby-talk [mailto:ruby-talk-bounces@ruby-lang.org] On Behalf Of Joe Tseng
Sent: 17 January 2017 20:05
To: Ruby users
Subject: [Rails/Postgres] DB migrate scripts break on references

I've been following online examples of how to create tables the Rails way and I noticed the migration scripts I created containing references don't work out of the box. I thought maybe I did something wrong so I tried just now making a test application/migration against a SQLite3 datasource. I ran the following commands:

$ rails g scaffold SchoolClass name
$ rails g scaffold Student name SchoolClass:references

And it made my migration scripts (along with everything else).
[Inline image 1]

I ran the migration scripts and it worked fine.
[Inline image 3]

When I tried doing the same steps above against a Postgres datasource, I kept on getting the following:

PG::UndefinedTable: ERROR: relation does not exist
The only way I got past this issue is if I went into the migration script and updated the t.references line and replaced the class name with the actual table name. It's a simple enough fix, but I've got a ton of tables to migrate and each table can have a good number of references. Has anyone seen this before, and is there some way to keep on using the scaffold utility to make migrations?

Appreciative of any useful assistance,

- Joe

--

If you type "Google" into Google, you can break the Internet. -- Jen Barber

Click here to view Company Information and Confidentiality Notice.<http://www.jameshall.co.uk/index.php/small-print/email-disclaimer>

Please note that we have updated our privacy policy in line with new data protection regulations. Please refer to our website to view the ways in which we handle your data.