pg-generator
pg-generator copied to clipboard
pgen command not working and not giving any message besides Socket is closed
Hello
I would like to thank you on this package very useful, and extremely handy,
I have been using it on previous version but recently have the following problem,
when I run the pgen command I get the internal command is about to executed but no feed back,
info: Executing command: pgen.js exec sequelize-main-template --host 192.168.0.13 --port 6432 --user admin --database mydb --schema main --target src/models/main --log info --extension nunj.html --indent 4 --env development
so I copied the command and it asks for a password, and then it says Socket is closed
Can you please help
I cannot repeat the problem. Did you possibly mistype the port? 6432
instead of default 5432
?
I am using a docker instance of PostgreSQL, so am able to connect to PostgreSQL from my node, java and IDE Applications, but when I use the package to generate the models, it keeps saying the socket is closed.
docker exposes the PostgreSQL instance on the host IP, with 6432 port as specified in the docker compose yml file
docker compose file
version: "3.7"
networks:
app_router:
driver: bridge
ipam:
config:
- subnet: 172.6.10.0/24
services:
app-db:
container_name: app_db
restart: always
build:
context: .
dockerfile: postgres.Dockerfile
networks:
app_router:
ipv4_address: 172.6.10.100
volumes:
- ./postgres/sqldump.sql:/docker-entrypoint-initdb.d/init.sql
- ${POSTGRES_HOME}:/home
- app_data:/var/lib/postgresql/data
ports:
- "6432:5432" #postgres port
environment:
POSTGRES_USER: admin
POSTGRES_PASSWORD: toor
POSTGRES_DB: mydb
PGDATA: /var/lib/postgresql/data
I didn't use it with docker. Could you please provide a repo and write steps to reproduce the issue, so I will investigate if I can help.
For everyone reaching this, I had this issue with node 14.X. Node 12.X seems to work fine.
@q8tywolf and @sfratini thanks for the case and warning.
I released a new major version of the pg-generator compatible with the latest Node, which is currently in the alpha stage. It is written from scratch.
What's New:
- In addition to CLI, provides API Written in TypeScript,
- Supports much more type of database objects (functions, materialized views, etc.)
- Supports any template engine, (examples are in Nunjucks though)
- Simpler for end-users,
- Simpler for creating your templates,
- Templates can be shared using npm,
- Supports environment variables and
.env
files for credentials. - Provides examples of the previous version as an npm module pg-generator-example
Also, documentation is completed. Please see pg-generator.com.
Example of using with objection2:
$ npm install -g [email protected] pg-generator-example
$ pgen example:objection2 --out-dir models --clear --relation-name-functions optimal"
Please provide your feedback if possible.
Kind Regards,
@ozum
Hello I am using the new alpha version I have few relation names that are duplicate, and I used the following command pgen example:sequelize6 --out-dir src/models/ --clear --relation-name-functions optimal
I still getting
Some relations have same names. Use "optimal" or "descriptive" for "relationNameFunctions" option or proivde your own naming functions.
Am I doing something wrong?
Regards
You are doing ok. It's normal to have name collisions in some complex database structures.
First try to use this option: --relation-name-functions descriptive
If you still get naming collision you have 2 ways:
a. (Easier) Try to rename foreign keys in your database. b. (Most flexible) Provide your own naming function as a node package described here: https://www.pg-generator.com/nav.01.guide/02.advanced.html#relation-names
@ozum
solution a. the foreign keys are unique in the database, but when you have table a and b
Table A id int name varchar
Table B table_a_id int -- forigen key name table_b_table_a_id table_a_id_x int -- forigen key name table_b_table_a_id_x table_a_id_y int -- forigen key name table_b_table_a_id_y table_a_id_z int -- forigen key name table_b_table_a_id_z
Some relations have same names. Use "optimal" or "descriptive" for "relationNameFunctions" option or proivde your own naming functions. { 'schema.table_b': { m2o: [ { listing: [ '[schema.table_b] ⭃―― table_b_table_a_id_x ―――[schema.table_a]', '[schema.table_b] ⭃―― table_b_table_a_id_y ―――[schema.table_a]', '[schema.table_b] ⭃―― table_b_table_a_id_z ―――[schema.table_a]' ] } ], o2m: [], m2m: [] }, 'schema.table_a': { m2o: [], o2m: [ { table_a_table_b: [ '[schema.table_a]――― table_b_table_a_id_x ――⥷ [schema.table_b]', '[schema.table_a]――― table_b_table_a_id_y ――⥷ [schema.table_b]', '[schema.table_a]――― table_b_table_a_id_z ――⥷ [schema.table_b]' ] } ], m2m: [ { table_a_table_a: [ '[schema.table_a]――― table_b_table_a_id_x ――⥷ [schema.table_b] ⭃―― schema.table_b.table_b_table_a_id_y ―――[schema.table_a]', '[schema.table_a]――― table_b_table_a_id_x ――⥷ [schema.table_b] ⭃―― schema.table_b.table_b_table_a_id_z ―――[schema.table_a]', '[schema.table_a]――― table_b_table_a_id_y ――⥷ [schema.table_b] ⭃―― schema.table_b.table_b_table_a_id_x ―――[schema.table_a]', '[schema.table_a]――― table_b_table_a_id_y ――⥷ [schema.table_b] ⭃―― schema.table_b.table_b_table_a_id_z ―――[schema.table_a]', '[schema.table_a]――― table_b_table_a_id_z ――⥷ [schema.table_b] ⭃―― schema.table_b.table_b_table_a_id_x ―――[schema.table_a]', '[schema.table_a]――― table_b_table_a_id_z ――⥷ [schema.table_b] ⭃―― schema.table_b.table_b_table_a_id_y ―――[schema.table_a]' ] } ] } }
See example schema below:
product
table has two foreign key columns: vendor_id
and alternative_vendor_id
product
table has two foreign keys: product_vendor
and product_alternative_vendor
You should rename your "foreign keys" not "columns of the foreign keys".
When you get this warning, it the command supposed to exit without generating any models? I would expect models without conflicting relation names to be generated.
Yes, this is on purpose. If there is a conflict, nothing is generated. Otherwise, you would have corrupted models.
Possible problems:
- FKs of non-generated relations refers to non-existing relation.
- Some models could refer to non-existing models.
- Some complex constraints refer to non-existing relations etc. etc.
Ok. In my case, I don't actually care about the models for which there are conflicting names. So an option to generate the models anyway or an option to exclude certain tables would be nice.
@ozum
Hello I am using the new alpha version I have few relation names that are duplicate, and I used the following command pgen example:sequelize6 --out-dir src/models/ --clear --relation-name-functions optimal
I still getting
Some relations have same names. Use "optimal" or "descriptive" for "relationNameFunctions" option or proivde your own naming functions.
Am I doing something wrong?
Regards
Hello @ozum
There is an issue with the alpha version when i run the process i get an error
for example
running below
pgen example:sequelize6 -i <schema_name> -o /home/dev/test-models --relationNameFunctions descriptive --clear
I get: `
pgen example:sequelize6 -i <schema_name> -o /home/dev/test-models --relationNameFunctions descriptive --clear
TypeError: Cannot read property 'table' of undefined
at ForeignKey.get referencedTable [as referencedTable] (/usr/local/lib/node_modules/pg-generator/node_modules/pg-structure/dist/pg-structure/constraint/foreign-key.js:29:27)
at /usr/local/lib/node_modules/pg-generator/node_modules/pg-structure/dist/main.js:288:28
at Array.forEach (
if I remove the -i and add
pgen sequelize6 -includeSchemas <schema_name> --out-dir /home/dev/test-models --relationNameFunctions descriptive --clear
You don't seem to have a generator with the name 'sequelize6' installed.
You should write the actual schema name instead of <schema_name>
. For example:
pgen example:sequelize6 -i public
In your second example, you
- forgot to add a second dash, and used a single dash. You should use
--
, not-
with named parameters. - forgot to add
example:
prefix.
pgen example:sequelize6 --includeSchemas public