When Trying to Generate a Model with Rails and Postgresql, the Command Hangs Without Error

When trying to generate a model with Rails and PostgreSQL, the command hangs without error

There is a bug in Spring with Rails 4.1 which causes generators to hang.

Try running spring stop and running the generator again.

Rails cannot generate model with mysql

Try stopping Spring with spring stop and then run the generator again.

When trying to generate a model with Rails and PostgreSQL, the command hangs without error

db query hangs forever in rails app

Comment out debugger in the show action. It is creating an breakpoint in your application. Alternatively if you open up your servers terminal window at this point where it is 'freezing' you will see some debug information, to allow the application to continue execution simply press CTRL-d in the terminal window.

Unable to run any command inside a rails project directory

Found the solution, It was the issue with the OpenSSL libraries on Catalina, Fixed by executing the following commands

ln -s /usr/local/Cellar/openssl@1.1/1.1.1d/lib/libcrypto.dylib /usr/local/lib/libcrypto.dylib
ln -s /usr/local/Cellar/openssl@1.1/1.1.1d/lib/libssl.dylib /usr/local/lib/libssl.dylib

if these commands executed without any error, everything should work.

Check the following gist

Connection to postgres from typescript hangs

NOTICE

TLDS! (TOO LONG DO SKIM)! The answer is long and rich! You can skim ! It's well formated!

If you are in big hurry!
You can check Authenticate section, Sequelize-typescript (not sequelize) section, Sequelize-typescript section.

And better you can go directly to HELL section! Get to know nodejs v14 HELL! (Go directly to the end! Well a bit above).

Check too FIX (Postgres v14 HELL)

I started and before knowing i found myself wrote too much!

SUPER GUIDE

Basically sequelize should not just hang! But throw an error!

Looking at code source

By looking at sync code here

 async sync(options) {
// ...

// no models defined, just authenticate
if (!models.length) {
await this.authenticate(options);
} else {
for (const model of models) await model.sync(options);
}
if (options.hooks) {
await this.runHooks('afterBulkSync', options);
}
return this;
}

One easily can see the hanging possiblities!

Logging

To debug such anomalies first of all it's important to have good logging!

And you can check how to add logging here! Even though normally sequelize have the logging of the query activated by default!

https://sequelize.org/master/manual/getting-started.html#logging

const sequelize = new Sequelize('sqlite::memory:', {
// Choose one of the logging options
logging: console.log, // Default, displays the first parameter of the log function call
logging: (...msg) => console.log(msg), // Displays all log function call parameters
logging: false, // Disables logging
logging: msg => logger.debug(msg), // Use custom logger (e.g. Winston or Bunyan), displays the first parameter
logging: logger.debug.bind(logger) // Alternative way to use custom logger, displays all messages
});

Check the authentication

If not logging happen! That may means that sequelize didn't anything and is just hanging at the start! To test authentication and if the connection is working!

You can test with authenticate:

https://sequelize.org/master/manual/getting-started.html#testing-the-connection

try {
console.log('Gonna authenticate'); // <== to make sure console.log is working and not overrided!
await sequelize.authenticate();
console.log('Connection has been established successfully.');
} catch (error) {
console.error('Unable to connect to the database:', error);
}

If you get no logging! And Gonna authenticate printed just ok! Then the process is hanging on authenticate. Which suggest a problem in authentication!

Check your credentials

Make sure you didn't make any mistakes!

Check connectivity from psql or some other external client

  • If it doesn't work! It suggest a problem with postgres server! Or some config!
  • If it does work! Then the problem is in nodejs! And your program!

DON'T FORGET TO INSTALL THE POSTGRES DRIVER (Or your db driver)

As per the doc: https://sequelize.org/master/manual/getting-started.html#installing

# One of the following:
$ npm install --save pg pg-hstore # Postgres
$ npm install --save mysql2
$ npm install --save mariadb
$ npm install --save sqlite3
$ npm install --save tedious # Microsoft SQL Server

One of the commands! Make sure you didn't forget that!

Understanding what's happening and debugging better! (code source logging)

The best way to debug! And really determine where the problem is happening! Is by adding logs to the source code itself! A fast way for me is to directly do that on node_modules. I opened git on sequelize repo! Made a search! Determined the place of sync, authenticate, query! All reside in sequelize.js! you can check here! One can CTRL + F to get to the methods > authenticate( [add the (]. Any way! You can do the same in node_modules! And start adding logs! You'll know in which part the problem is happening! Which help you debug the problem!

The other way is to fork! And use your fork! And just work better!

But mmm! node_modules is a fast way! You can take a copy! too! To make sure you don't loose your logs! Once you update! At the end clean by just removing the whole module! And reinstall! Or just reverse the logs creation (undo)! I find it an interesting way for debugging!

But it should throw an error

Normally it should! By seeing the code source you can know better! Normally an error should be thrown! But if a process get hanged! And no error is thrown! Then you can expect a result like this! Here that can be the driver missing! Also make sure console.log. Is working all right! And the last thing! MMM may be it's a problem with nodejs itself (see the last section).

I'm using sequelize-typescript (not sequelize)

Very important to know! Sequelize-typescript is just a sequelize wrapper! That was there to add typescript support! It provide decorators and some features! Also From sequelize v5 ! Typescript is supported directly in sequelize! See here
https://sequelize.org/master/manual/typescript.html
sequelize-typescript in the latest version! Turned to use too the native declaration types of sequelize!

As sequelize-typecript wrap sequelize! Make sure to verify sequelize documentation!

To note too there is some people shouting: Don't use decorators! mmmm! mmm! And another mmmm!
https://stackoverflow.com/a/60369189/7668448

Sequelize-typescript

If you are using sequelize-typescript make sure the version of sequelize-typescript and sequelize do match! As per there doc V5 of sequelize! I guess V6 too should do! And v1 for sequelize-typescript!

npm install sequelize@5 sequelize-typescript@1

And don't forget the typescript needed packages as per there doc!

https://www.npmjs.com/package/sequelize-typescript

(You can check and verify all those information in the doc itself)

Why to use sequelize-typescript ?

As already mentionned! Sequelize have native support for typescript starting from V5. As per here. So why use a wrapper above it! That use decorators too! (I'm not against decorators! Some are! As per here)

Ask yourself why ? Is there anything to sequelize-typescript! An important plus comparing to the native way ? If there is clear things! Please mention them in the comments! And i'll update! This section!

And if not! Native can be way better! A dependency or many in less!

Project configuration

tsconfig!

{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"moduleResolution": "node",
"rootDir": "./src",
"outDir": "./dist",
"lib": [
"es2015",
"es2016",
"dom"
],
"declaration": true,
"experimentalDecorators": true,
"esModuleInterop": true
},
"include": [
"src/**/*"
],
"exclude": [
"node_modules/**/*",
"src/**/*.test.tsx",
"src/**/*.story.tsx",
"test/**/*"
]
}

Those are the two important things to add.

"experimentalDecorators": true,
"esModuleInterop": true

But that should not be your problem! Otherwise the project will throw compile errors!

HELL

Node js VERSION (V14 HELL)

And yea! That can be the cause! You may already used to have it work all right before! And then in a new computer or environment! It's not working any more!

Node version can be the problem! Node v15 and Node v14! That's a known problem! I myself encountered once with knex.js and postgres (knex.js is a query builder)! So you can see that is related! In my story the code was working all right on my laptop and old vps that we deployed in! But then i deployed on a windows rdp! And mmm! Boom! Then i pulled my hair for some time! I reflected and checked! There was no change! And then i came to hey! I only updated nodejs! And later i found that other people encountered the same thing! In short! It all started on nodejs v14 (i call this v14 HELL)! You can check my answer about it here

And apparently the same problem is always there with nodejs v15!

In the question of this thread! We confirmed that! In my desktop all worked well! Nodejs v12! And with my friend computer! It didn't! nodejs v14 and nodejs v15. Then i wanted to confirm! I installed nodejs v15 And caboom! BINGO! The execution just stop unexpectedly! No logging ! No error! In v12! All was working correctly! i had errors at first then i corrected them! And the server was up and running! And sequelize connected to the DB!

Here the executions

V12 and v13

I'm displaying v13! The same happen with v12!

Sample Image

nvm use v13
Now using node v13.14.0 (npm v6.14.4)
coderhero@HeroStation  ~/Documents/coderhero/Dev/projects/Fahima-ecommerce   LuckyLook  npm run dev

> bagsshoes-server@1.0.0 dev /home/coderhero/Documents/coderhero/Dev/projects/Fahima-ecommerce
> npx ts-node-dev src/server.ts

[INFO] 01:49:29 ts-node-dev ver. 1.0.0 (using ts-node ver. 9.0.0, typescript ver. 4.0.5)
config ::::
{
username: 'fahima',
password: '123456',
database: 'fahimashop',
host: 'localhost',
dialect: 'postgres'
}
hi there ::::
Executing (default): SELECT 1+1 AS result
connection established
Executing (default): CREATE TABLE IF NOT EXISTS "Products" ("id" SERIAL , "brand" TEXT, "price" DECIMAL, "description" VARCHAR(255), "imgUrl" VARCHAR(255), "category" VARCHAR(255), "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL, "updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL, PRIMARY KEY ("id"));
Executing (default): SELECT i.relname AS name, ix.indisprimary AS primary, ix.indisunique AS unique, ix.indkey AS indkey, array_agg(a.attnum) as column_indexes, array_agg(a.attname) AS column_names, pg_get_indexdef(ix.indexrelid) AS definition FROM pg_class t, pg_class i, pg_index ix, pg_attribute a WHERE t.oid = ix.indrelid AND i.oid = ix.indexrelid AND a.attrelid = t.oid AND t.relkind = 'r' and t.relname = 'Products' GROUP BY i.relname, ix.indexrelid, ix.indisprimary, ix.indisunique, ix.indkey ORDER BY i.relname;
server running http://localhost:8100
press CTRL+C to stop server, please :)

Cool working no problem

V14 and v15 execution

v14

Sample Image

 coderhero@HeroStation  ~/Documents/coderhero/Dev/projects/Fahima-ecommerce   LuckyLook  node -v        
v14.15.0
coderhero@HeroStation  ~/Documents/coderhero/Dev/projects/Fahima-ecommerce   LuckyLook  npm run dev

> bagsshoes-server@1.0.0 dev /home/coderhero/Documents/coderhero/Dev/projects/Fahima-ecommerce
> npx ts-node-dev src/server.ts

[INFO] 02:07:35 ts-node-dev ver. 1.0.0 (using ts-node ver. 9.0.0, typescript ver. 4.0.5)
config ::::
{
username: 'fahima',
password: '123456',
database: 'fahimashop',
host: 'localhost',
dialect: 'postgres'
}
hi there ::::
coderhero@HeroStation  ~/Documents/coderhero/Dev/projects/Fahima-ecommerce   LuckyLook 

And opps! The program is exiting unexpectedly with no error output!

V15

Sample Image

 coderhero@HeroStation  ~/Documents/coderhero/Dev/projects/Fahima-ecommerce   LuckyLook  nvm use v15    
Now using node v15.0.1 (npm v7.0.3)
coderhero@HeroStation  ~/Documents/coderhero/Dev/projects/Fahima-ecommerce   LuckyLook  npm run dev

> bagsshoes-server@1.0.0 dev
> npx ts-node-dev src/server.ts

[INFO] 02:10:48 ts-node-dev ver. 1.0.0 (using ts-node ver. 9.0.0, typescript ver. 4.0.5)
config ::::
{
username: 'fahima',
password: '123456',
database: 'fahimashop',
host: 'localhost',
dialect: 'postgres'
}
hi there ::::
coderhero@HeroStation  ~/Documents/coderhero/Dev/projects/Fahima-ecommerce   LuckyLook 

And oppsii again! The program is exiting unexpectedly with no error output!

There is no difference too between v14 and v15! It's V14 HELL.

In short

The V14 HELL is a known and very probable cause! There is a problem with pg module i guess! Something changed on v14 and caused this problem!

In short short! If nothing is making any sense! And if your same code was working before! First thing to do! Is to check with nodejs v13 or v12! That can save you from insanity! Who would say the version of nodejs and a new one will create such a problem!

What is this problem! What is V14 HELL in nodejs ?

If like me you like to know the details and what did happen !?

With node V14! Some breaking changes happened on the api! Also many things were changed! Including Openssl version!

For postgres! And pg module! The problem was as described in this comment per this thread:

The initial readyState (a private/undocumented API that

pg uses) of net.Socket seems to have changed from 'closed' to 'open'
in Node 14.

It’s hard to fix with perfect backwards compatibility, but I think I
have a patch that’s close enough.

And as per this PR!

You can see the changes in this diffing

In short as mentioned! The behavior for onReadySate changed for net.Socket !
And the implemented solution was to not use onReadyState at all!

And as per this

Connection now always calls connect on its stream when connect is called on it.

Check this line

In the older version the connect was called only if the socket is on closed state! readyState usage is eliminated!

You can understand! Depending on the implementation! Many things may or not be affected by those core changes!

Relevant node changes

And because i wanted to see where the change happen! Here you go and can check

https://github.com/nodejs/node/pull/32272

One can check the log of changes too:

https://github.com/nodejs/node/blob/master/doc/changelogs/CHANGELOG_V14.md

FIX (Postgres v14 HELL)

As per my answer here.

Upgrade pg driver to >=8.0.3! You can simply upgrade to latest!

npm install pg@latest --save

And you can check for our problem in question

Old version at v7

Sample Image

Updating to v8

Sample Image

Running again with node v15

Sample Image

Taraaaaa! And it worked awesomely!

And if you are not using postgres! And the problem was v14 HELL! Meaning You tested with v13. And it worked! Then try to upgrade your db driver to latest!

Why node v14 + exit and no logging error

Also to mention the breaking changes! Made pg make the process exit at connect() call. And that's what made it exit! And logging was to be seen!
In more detail for this! Here how it happened! Sequelize have the postgres dialect implementation! Which use pg! And pg client! create a connection! The connection have a connect event! When it connect it emit it! And because node v14 change the behavior to starting with open! The stream connection is skipped! And the stream is taken as connected! Where it is not! And the connect event is emitted directly! When that happen! The client either will call requestSsl() or startup() method of the connection object! And both will call this._stream.write. because the stream is not connected! An error happen! This error is not catch! Then the promise in sequelize driver! Will stay unresolved! And then the event loop get empty! Nodejs by default behavior just exit!

You can see the step through lines of code:

  • Sequelize pg adapter will call pg client to create a connection and the promise
  • pg client call connect on a connection object
  • pg connection connect() call and emit connect! Thinking the stream is connected because of V14 change
  • pg client connect event catched and callback run! requestSsl() or startup() will be run
  • One of the method get run and stream.write will be called (requestSsl(), startup())
  • Stream Error (not catched)
  • Promise in sequelize postgres adapter! Still unresolved!
  • event loop empty => Nodejs => Exit

Why nodejs exit (unresolved promises)

https://github.com/nodejs/node/issues/22088

Node exits without error and doesn't await promise (Event callback)

what happens when a Promise never resolves?

NVM

https://github.com/nvm-sh/nvm

If you don't know what nvm is! Or you are not using nvm. Consider using it! As it's a very interesting tool! Nvm is a node version management tool!

With nvm changing, debugging and testing to and with different version of nodejs! Is quick and a breeze! And so installing new versions of nodejs in parallel!

Note about sequelize.sync()

Do not use it for production! Or at all! (Most of ORM! And query builder (knex.js) use migrations).

https://sequelize.org/master/manual/model-basics.html#synchronization-in-production

From the doc

As shown above, sync({ force: true }) and sync({ alter: true }) can be
destructive operations. Therefore, they are not recommended for
production-level software. Instead, synchronization should be done
with the advanced concept of Migrations, with the help of the
Sequelize CLI.

Issue with controller generation in rails

For anyone stumbling across this question, the correct answer is that the database need not exist, but it must be properly configured as if it did exist in the config file. Generating the controller does not actually create the database.

Bizarre PG::UndefinedTable Error with Rails app on Heroku

I FIGURED IT OUT

Thanks a ton, mu is too short. I think my issue was that I was referencing my guest class in my initializer file. I moved that bit of code to the controller method that it was relevant to, pushed to heroku, and everything is working fine now.



Related Topics



Leave a reply



Submit