Model, after some time, decides it references a different (non-existent) table

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Model, after some time, decides it references a different (non-existent) table

Phil Edelbrock

This is odd... I have a few models that after some time just start doing SELECT "my_tablemy_belongs_to_table".* sort of thing. (Almost like it is trying to reference a join table, but missing a _ between the table names.)

In other words, things work for hours, days or weeks then suddenly goes from SELECT "my_table".* to SELECT "my_tablemy_belongs_to_table".*

The associated FROM clause also changes to match.  Hundreds of exceptions per minute then ensue and all heck breaks loose. ;')

I perhaps am running into a namespace issue or something(?)... how does a model get triggered into changing it's associated table name in runtime?  Our temp fix it to just restart the service/app but that's obviously not sustainable.  We had to do it several times today.

Thoughts?

ruby 2.5.0p21 (2018-02-18 revision 62472) [x86_64-linux]
rails 5.1.6
pg 1.0.0

Thanks!


Phil

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/06C283DC-D65C-4F5D-AD30-2D620B48242F%40gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Model, after some time, decides it references a different (non-existent) table

Colin Law
On 28 June 2018 at 02:13, Phil Edelbrock <[hidden email]> wrote:
>
> This is odd... I have a few models that after some time just start doing SELECT "my_tablemy_belongs_to_table".* sort of thing. (Almost like it is trying to reference a join table, but missing a _ between the table names.)

Is there nothing unusual in the log just before it happens the first
time?  Also look in the system/server logs to see if anything happened
there.

Colin

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLsmZz8YFoe9Qh%2BUYJWWOVBV_j%3DaKvuioS3_CJrrarTu1g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Model, after some time, decides it references a different (non-existent) table

Frederick Cheung-2
In reply to this post by Phil Edelbrock
On Thursday, June 28, 2018 at 2:13:31 AM UTC+1, Phil wrote:
> This is odd... I have a few models that after some time just start doing SELECT "my_tablemy_belongs_to_table".* sort of thing. (Almost like it is trying to reference a join table, but missing a _ between the table names.)
>
> In other words, things work for hours, days or weeks then suddenly goes from SELECT "my_table".* to SELECT "my_tablemy_belongs_to_table".*

There are a few activerecord settings (table_name, table_name_prefix maybe some others) that could do this. Might these be getting changed by accident (perhaps because your model has a method whose name clashes with an active record method) ?

Fred

>
> The associated FROM clause also changes to match.  Hundreds of exceptions per minute then ensue and all heck breaks loose. ;')
>
> I perhaps am running into a namespace issue or something(?)... how does a model get triggered into changing it's associated table name in runtime?  Our temp fix it to just restart the service/app but that's obviously not sustainable.  We had to do it several times today.
>
> Thoughts?
>
> ruby 2.5.0p21 (2018-02-18 revision 62472) [x86_64-linux]
> rails 5.1.6
> pg 1.0.0
>
> Thanks!
>
>
> Phil
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/1ba53edc-d9c8-47e0-9811-5834b20548f8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Model, after some time, decides it references a different (non-existent) table

Phil Edelbrock


> On Jun 29, 2018, at 11:17 PM, Frederick Cheung <[hidden email]> wrote:
>
> On Thursday, June 28, 2018 at 2:13:31 AM UTC+1, Phil wrote:
>> This is odd... I have a few models that after some time just start doing SELECT "my_tablemy_belongs_to_table".* sort of thing. (Almost like it is trying to reference a join table, but missing a _ between the table names.)
>>
>> In other words, things work for hours, days or weeks then suddenly goes from SELECT "my_table".* to SELECT "my_tablemy_belongs_to_table".*
>
> There are a few activerecord settings (table_name, table_name_prefix maybe some others) that could do this. Might these be getting changed by accident (perhaps because your model has a method whose name clashes with an active record method) ?
>
> Fred
>
>>
>> The associated FROM clause also changes to match.  Hundreds of exceptions per minute then ensue and all heck breaks loose. ;')
>>
>> I perhaps am running into a namespace issue or something(?)... how does a model get triggered into changing it's associated table name in runtime?  Our temp fix it to just restart the service/app but that's obviously not sustainable.  We had to do it several times today.
>>
>> Thoughts?
>>
>> ruby 2.5.0p21 (2018-02-18 revision 62472) [x86_64-linux]
>> rails 5.1.6
>> pg 1.0.0
>>
>> Thanks!
>>
>>
>> Phil

Thank you Fred and Colin.

Somehow table_name seems to be getting set to something else... I can simulate how it breaks but I don't know where/why it is breaking in the production code...

>> UserAccount.table_name = "blah"
=> "blah"
>> UserAccount.first
Traceback (most recent call last):
        1: from (irb):2
ActiveRecord::StatementInvalid (PG::UndefinedTable: ERROR:  relation "blah" does not exist
LINE 1: SELECT  "blah".* FROM "blah" ORDER BY "blah"."id" ASC LIMIT ...
                              ^
: SELECT  "blah".* FROM "blah" ORDER BY "blah"."id" ASC LIMIT $1)


Something in Rails 5.1.x is appending to 'table_name'  in the model during some condition?

I can't easily reproduce.  I don't see anywhere in the code where I'm operating on table_name directly.

(still digging, I hope everyone is having a good Saturday, btw :')


Phil

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/066091BE-E396-4E3A-BDDC-DE0DECD24476%40gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Model, after some time, decides it references a different (non-existent) table

Phil Edelbrock


> On Jun 30, 2018, at 9:53 AM, Phil Edelbrock <[hidden email]> wrote:
>
>
>
>> On Jun 29, 2018, at 11:17 PM, Frederick Cheung <[hidden email]> wrote:
>>
>> On Thursday, June 28, 2018 at 2:13:31 AM UTC+1, Phil wrote:
>>> This is odd... I have a few models that after some time just start doing SELECT "my_tablemy_belongs_to_table".* sort of thing. (Almost like it is trying to reference a join table, but missing a _ between the table names.)
>>>
>>> In other words, things work for hours, days or weeks then suddenly goes from SELECT "my_table".* to SELECT "my_tablemy_belongs_to_table".*
>>
>> There are a few activerecord settings (table_name, table_name_prefix maybe some others) that could do this. Might these be getting changed by accident (perhaps because your model has a method whose name clashes with an active record method) ?
>>
>> Fred
>>
>>>
>>> The associated FROM clause also changes to match.  Hundreds of exceptions per minute then ensue and all heck breaks loose. ;')
>>>
>>> I perhaps am running into a namespace issue or something(?)... how does a model get triggered into changing it's associated table name in runtime?  Our temp fix it to just restart the service/app but that's obviously not sustainable.  We had to do it several times today.
>>>
>>> Thoughts?
>>>
>>> ruby 2.5.0p21 (2018-02-18 revision 62472) [x86_64-linux]
>>> rails 5.1.6
>>> pg 1.0.0
>>>
>>> Thanks!
>>>
>>>
>>> Phil
>
> Thank you Fred and Colin.
>
> Somehow table_name seems to be getting set to something else... I can simulate how it breaks but I don't know where/why it is breaking in the production code...
>
>>> UserAccount.table_name = "blah"
> => "blah"
>>> UserAccount.first
> Traceback (most recent call last):
>        1: from (irb):2
> ActiveRecord::StatementInvalid (PG::UndefinedTable: ERROR:  relation "blah" does not exist
> LINE 1: SELECT  "blah".* FROM "blah" ORDER BY "blah"."id" ASC LIMIT ...
>                              ^
> : SELECT  "blah".* FROM "blah" ORDER BY "blah"."id" ASC LIMIT $1)
>
>
> Something in Rails 5.1.x is appending to 'table_name'  in the model during some condition?
>
> I can't easily reproduce.  I don't see anywhere in the code where I'm operating on table_name directly.
>
> (still digging, I hope everyone is having a good Saturday, btw :')
>
>
> Phil


Ah, I've solved this.  We've migrated code that used arrays, to strings.  We were still using "<<", though, to concatenate strings.  Let me give you some examples to understand the issue....

 >> a = MyItem.table_name
 => "my_items"
 >> b = a
 => "my_items"
 >> c = b
 => "my_items"
 >> d = c
 => "my_items"
 >> d += ", other_items"
 => "my_items, other_items"
 >> d
 => "my_items, other_items"
 >> c
 => "my_items"

All good, right?  Now, let's use '<<':

 >> a = MyItem.table_name
 => "my_items"
 >> b = a
 => "my_items"
 >> c = b
 => "my_items"
 >> d = c
 => "my_items"
 >> d << ", other_items"
 => "my_items, other_items"     <---- Same result!  Looks right.
 >> c
 => "my_items, other_items"
 >> MyTable.table_name
 => "my_items, other_items"  <--- Oh noes, the result was inherited all the way down to the table_name of the model!

Now the MyItem model is borked until the app is restarted.

The malformed 'from' clause was a bit of a red herring and that was easily resolved, btw. (A 'join(', ')' was skipped because the construction of the from-clause was a string not an array any more in our code.)

The '<<' method appears to work the same as the String method ".concat(something)" .  It also makes all other variables inherit the same value.

Another possible solution is to use '.dup', for what it is worth:

 >> d = c.dup
 => "my_items"
 >> d << ", other_items"
 => "my_items, other_items"
 >> c
 => "my_items"

I hope this helps somebody if they run into a similar problem!  (I don't claim this is a Ruby bug or such, just perhaps not very well documented feature.)


Phil


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/53D0ED59-95F0-4602-B71C-F35C1284F738%40gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Model, after some time, decides it references a different (non-existent) table

botp

On Tue, Jul 3, 2018 at 7:03 AM, Phil Edelbrock <[hidden email]> wrote:
 >> a = MyItem.table_name
 => "my_items"
 >> b = a
 => "my_items"
 >> c = b
 => "my_items"
 >> d = c
 => "my_items"

​var references object.​
at this point, all vars a,b,c,d points to MyItem.table_name
 
 >> d << ", other_items"
 => "my_items, other_items"     <---- Same result!  Looks right.
 >> c
 => "my_items, other_items"
 >> MyTable.table_name
 => "my_items, other_items"  <--- Oh noes, the result was inherited all the way down to the table_name of the model!

Now the MyItem model is borked until the app is restarted.

The malformed 'from' clause was a bit of a red herring and that was easily resolved, btw. (A 'join(', ')' was skipped because the construction of the from-clause was a string not an array any more in our code.)

The '<<' method appears to work the same as the String method ".concat(something)" .  It also makes all other variables inherit the same value.


it does more, ​just be careful since it flattens.


(from ruby core)
=== Implementation from Array
------------------------------------------------------------------------
  ary.concat(other_ary1, other_ary2,...)   -> ary

------------------------------------------------------------------------

Appends the elements of +other_ary+s to self.

  [ "a", "b" ].concat( ["c", "d"] ) #=> [ "a", "b", "c", "d" ]
  [ "a" ].concat( ["b"], ["c", "d"] ) #=> [ "a", "b", "c", "d" ]
  [ "a" ].concat #=> [ "a" ]

 
Another possible solution is to use '.dup', for what it is worth:


​indeed

 >> d = c.dup
 => "my_items"
 >> d << ", other_items"
 => "my_items, other_items"
 >> c
 => "my_items"

I hope this helps somebody if they run into a similar problem!  (I don't claim this is a Ruby bug or such, just perhaps not very well documented feature.)


(from ruby core)
=== Implementation from Array
------------------------------------------------------------------------
  ary << obj            -> ary

------------------------------------------------------------------------

Append---Pushes the given object on to the end of this array. This
expression returns the array itself, so several appends may be chained
together.

  [ 1, 2 ] << "c" << "d" << [ 3, 4 ]
          #=>  [ 1, 2, "c", "d", [ 3, 4 ] ]

fwiw, you're function/method seems to do a lot of things. maybe best to create a unit test for it, no?
 

Phil

​kind regards​
,
--botp

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAAwHHQjCS8zL46U7ygrViqUj%2BrW1cRdW7ejptVVWHSXH8TrTLA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.