ozum / pg-generator

Template Based Scaffolding Tool for PostgreSQL. Create files and ORM models automatically.
http://www.pg-generator.com
MIT License
91 stars 21 forks source link

pgen command not working and not giving any message besides Socket is closed #70

Open q8tywolf opened 4 years ago

q8tywolf commented 4 years ago

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

ozum commented 4 years ago

I cannot repeat the problem. Did you possibly mistype the port? 6432 instead of default 5432?

q8tywolf commented 4 years ago

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
ozum commented 4 years ago

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.

sfratini commented 3 years ago

For everyone reaching this, I had this issue with node 14.X. Node 12.X seems to work fine.

ozum commented 3 years ago

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

Also, documentation is completed. Please see pg-generator.com.

Example of using with objection2:

$ npm install -g pg-generator@5.0.0-alpha.8 pg-generator-example
$ pgen example:objection2 --out-dir models --clear --relation-name-functions optimal"

Please provide your feedback if possible.

Kind Regards,

q8tywolf commented 3 years ago

@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

ozum commented 3 years ago

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

q8tywolf commented 3 years ago

@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]' ] } ] } }

ozum commented 3 years ago

See example schema below:

image

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

blevine commented 2 years ago

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.

ozum commented 2 years ago

Yes, this is on purpose. If there is a conflict, nothing is generated. Otherwise, you would have corrupted models.

Possible problems:

blevine commented 2 years ago

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.

q8tywolf commented 2 years ago

@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 -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 () at addConstraints (/usr/local/lib/node_modules/pg-generator/node_modules/pg-structure/dist/main.js:262:10) at addObjects (/usr/local/lib/node_modules/pg-generator/node_modules/pg-structure/dist/main.js:336:5) at Object.pgStructure [as default] (/usr/local/lib/node_modules/pg-generator/node_modules/pg-structure/dist/main.js:375:5) at processTicksAndRejections (internal/process/task_queues.js:95:5) at async Object.getDb (/usr/local/lib/node_modules/pg-generator/dist/utils/get-db.js:25:16) at async Object.generate (/usr/local/lib/node_modules/pg-generator/dist/generate.js:8:16) at async executeCommand (/usr/local/lib/node_modules/pg-generator/dist/bin/pgen.js:35:16) `

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.
ozum commented 2 years ago

You should write the actual schema name instead of <schema_name>. For example:

pgen example:sequelize6 -i public

In your second example, you

pgen example:sequelize6 --includeSchemas public