Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
q8tywolf opened this issue Jul 5, 2020 · 14 comments

Comments

@q8tywolf
Copy link

q8tywolf commented Jul 5, 2020

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
Copy link
Owner

ozum commented Jul 5, 2020

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

@q8tywolf
Copy link
Author

q8tywolf commented Jul 6, 2020

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
Copy link
Owner

ozum commented Jul 6, 2020

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
Copy link

sfratini commented Nov 6, 2020

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

@ozum
Copy link
Owner

ozum commented Mar 13, 2021

@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 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
Copy link
Author

@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
Copy link
Owner

ozum commented May 18, 2021

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
Copy link
Author

q8tywolf commented May 19, 2021

@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
Copy link
Owner

ozum commented May 19, 2021

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
Copy link

blevine commented Sep 28, 2021

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
Copy link
Owner

ozum commented Sep 28, 2021

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.

@blevine
Copy link

blevine commented Sep 28, 2021

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
Copy link
Author

@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 ()
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
Copy link
Owner

ozum commented Oct 13, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants