nodejh / sequelize-automate

Automatically generate bare sequelize models from your database.
MIT License
114 stars 21 forks source link

TypeError: Property value expected type of string but got null #7

Closed athenawisdoms closed 4 years ago

athenawisdoms commented 4 years ago

Hi,

Firstly, thank you for this wonderful (and updated) work!

I am encountering an error when I try to use sequelize-automate (v1.0.1, installed globally) in the terminal on a database which has an existing table with no rows. However, it is failing with an error and no model files are written to disk:

TypeError: Property value expected type of string but got null

Do you happen to know how we can solve this? I am new to Sequelize, please let me know if I left out any important information that is needed to troubleshoot this. Thanks!

System

Command used:

% sequelize-automate -t js -h localhost -d database_development -u postgres -p password --port 5432 --dialect postgres -o models

Entire output:

Executing (default): SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' AND table_type LIKE '%TABLE' AND table_name != 'spatial_ref_sys';
Executing (default): SELECT pk.constraint_type as "Constraint",c.column_name as "Field", c.column_default as "Default",c.is_nullable as "Null", (CASE WHEN c.udt_name = 'hstore' THEN c.udt_name ELSE c.data_type END) || (CASE WHEN c.character_maximum_length IS NOT NULL THEN '(' || c.character_maximum_length || ')' ELSE '' END) as "Type", (SELECT array_agg(e.enumlabel) FROM pg_catalog.pg_type t JOIN pg_catalog.pg_enum e ON t.oid=e.enumtypid WHERE t.typname=c.udt_name) AS "special", (SELECT pgd.description FROM pg_catalog.pg_statio_all_tables AS st INNER JOIN pg_catalog.pg_description pgd on (pgd.objoid=st.relid) WHERE c.ordinal_position=pgd.objsubid AND c.table_name=st.relname) AS "Comment" FROM information_schema.columns c LEFT JOIN (SELECT tc.table_schema, tc.table_name, cu.column_name, tc.constraint_type FROM information_schema.TABLE_CONSTRAINTS tc JOIN information_schema.KEY_COLUMN_USAGE  cu ON tc.table_schema=cu.table_schema and tc.table_name=cu.table_name and tc.constraint_name=cu.constraint_name and tc.constraint_type='PRIMARY KEY') pk ON pk.table_schema=c.table_schema AND pk.table_name=c.table_name AND pk.column_name=c.column_name WHERE c.table_name = 'SequelizeMeta' AND c.table_schema = 'public'
Executing (default): SELECT pk.constraint_type as "Constraint",c.column_name as "Field", c.column_default as "Default",c.is_nullable as "Null", (CASE WHEN c.udt_name = 'hstore' THEN c.udt_name ELSE c.data_type END) || (CASE WHEN c.character_maximum_length IS NOT NULL THEN '(' || c.character_maximum_length || ')' ELSE '' END) as "Type", (SELECT array_agg(e.enumlabel) FROM pg_catalog.pg_type t JOIN pg_catalog.pg_enum e ON t.oid=e.enumtypid WHERE t.typname=c.udt_name) AS "special", (SELECT pgd.description FROM pg_catalog.pg_statio_all_tables AS st INNER JOIN pg_catalog.pg_description pgd on (pgd.objoid=st.relid) WHERE c.ordinal_position=pgd.objsubid AND c.table_name=st.relname) AS "Comment" FROM information_schema.columns c LEFT JOIN (SELECT tc.table_schema, tc.table_name, cu.column_name, tc.constraint_type FROM information_schema.TABLE_CONSTRAINTS tc JOIN information_schema.KEY_COLUMN_USAGE  cu ON tc.table_schema=cu.table_schema and tc.table_name=cu.table_name and tc.constraint_name=cu.constraint_name and tc.constraint_type='PRIMARY KEY') pk ON pk.table_schema=c.table_schema AND pk.table_name=c.table_name AND pk.column_name=c.column_name WHERE c.table_name = 'test_foobar' AND c.table_schema = 'public'
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 = 'SequelizeMeta' GROUP BY i.relname, ix.indexrelid, ix.indisprimary, ix.indisunique, ix.indkey ORDER BY i.relname;
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 = 'test_foobar' GROUP BY i.relname, ix.indexrelid, ix.indisprimary, ix.indisunique, ix.indkey ORDER BY i.relname;
Executing (default): SELECT DISTINCT tc.constraint_name as constraint_name, tc.constraint_schema as constraint_schema, tc.constraint_catalog as constraint_catalog, tc.table_name as table_name,tc.table_schema as table_schema,tc.table_catalog as table_catalog,kcu.column_name as column_name,ccu.table_schema  AS referenced_table_schema,ccu.table_catalog  AS referenced_table_catalog,ccu.table_name  AS referenced_table_name,ccu.column_name AS referenced_column_name FROM information_schema.table_constraints AS tc JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name JOIN information_schema.constraint_column_usage AS ccu ON ccu.constraint_name = tc.constraint_name WHERE constraint_type = 'FOREIGN KEY' AND tc.table_name = 'SequelizeMeta' AND tc.table_catalog = 'database_development'
Executing (default): SELECT DISTINCT tc.constraint_name as constraint_name, tc.constraint_schema as constraint_schema, tc.constraint_catalog as constraint_catalog, tc.table_name as table_name,tc.table_schema as table_schema,tc.table_catalog as table_catalog,kcu.column_name as column_name,ccu.table_schema  AS referenced_table_schema,ccu.table_catalog  AS referenced_table_catalog,ccu.table_name  AS referenced_table_name,ccu.column_name AS referenced_column_name FROM information_schema.table_constraints AS tc JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name JOIN information_schema.constraint_column_usage AS ccu ON ccu.constraint_name = tc.constraint_name WHERE constraint_type = 'FOREIGN KEY' AND tc.table_name = 'test_foobar' AND tc.table_catalog = 'database_development'
TypeError: Property value expected type of string but got null
    at Object.validate (/usr/local/lib/node_modules/sequelize-automate/node_modules/@babel/types/lib/definitions/utils.js:164:13)
    at validateField (/usr/local/lib/node_modules/sequelize-automate/node_modules/@babel/types/lib/validators/validate.js:22:9)
    at validate (/usr/local/lib/node_modules/sequelize-automate/node_modules/@babel/types/lib/validators/validate.js:16:3)
    at builder (/usr/local/lib/node_modules/sequelize-automate/node_modules/@babel/types/lib/builders/builder.js:38:27)
    at Object.StringLiteral (/usr/local/lib/node_modules/sequelize-automate/node_modules/@babel/types/lib/builders/generated/index.js:346:31)
    at /usr/local/lib/node_modules/sequelize-automate/src/generate/common/index.js:146:19
    at arrayMap (/usr/local/lib/node_modules/sequelize-automate/node_modules/lodash/lodash.js:639:23)
    at Function.map (/usr/local/lib/node_modules/sequelize-automate/node_modules/lodash/lodash.js:9554:14)
    at /usr/local/lib/node_modules/sequelize-automate/src/generate/common/index.js:134:13
    at Array.map (<anonymous>)
    at processOptionsProperties (/usr/local/lib/node_modules/sequelize-automate/src/generate/common/index.js:123:16)
    at VariableDeclarator (/usr/local/lib/node_modules/sequelize-automate/src/generate/javascript.js:39:28)
    at NodePath._call (/usr/local/lib/node_modules/sequelize-automate/node_modules/@babel/traverse/lib/path/context.js:55:20)
    at NodePath.call (/usr/local/lib/node_modules/sequelize-automate/node_modules/@babel/traverse/lib/path/context.js:42:17)
    at NodePath.visit (/usr/local/lib/node_modules/sequelize-automate/node_modules/@babel/traverse/lib/path/context.js:90:31)
    at TraversalContext.visitQueue (/usr/local/lib/node_modules/sequelize-automate/node_modules/@babel/traverse/lib/context.js:112:16)
    at TraversalContext.visitMultiple (/usr/local/lib/node_modules/sequelize-automate/node_modules/@babel/traverse/lib/context.js:79:17)
    at TraversalContext.visit (/usr/local/lib/node_modules/sequelize-automate/node_modules/@babel/traverse/lib/context.js:138:19)
    at Function.traverse.node (/usr/local/lib/node_modules/sequelize-automate/node_modules/@babel/traverse/lib/index.js:84:17)
    at NodePath.visit (/usr/local/lib/node_modules/sequelize-automate/node_modules/@babel/traverse/lib/path/context.js:97:18)
    at TraversalContext.visitQueue (/usr/local/lib/node_modules/sequelize-automate/node_modules/@babel/traverse/lib/context.js:112:16)
    at TraversalContext.visitMultiple (/usr/local/lib/node_modules/sequelize-automate/node_modules/@babel/traverse/lib/context.js:79:17)
nodejh commented 4 years ago

Thanks for your feedback, maybe it's a bug. I will fix it as soon as possible.

nodejh commented 4 years ago

@athenawisdoms Hi, I have fixed this issue and published the new version sequelize-automate@1.1.1. You can reinstall the package and try again.

It's caused by the indexes of PostgreSQL. I use queryInterface.showIndex() (a sequelize method) to get all indexes of a table, but the results are different of different database dialects. For example, the results of MySQL have a type property, but PostgreSQL dose not. And I pass type to t.identifier('type'), so that I can generate an identifier of AST. While type is undefined, t.identifier throw that error: TypeError: Property value expected type of string but got null.

If you have any problem, please let me know.

athenawisdoms commented 4 years ago

Perfect, everything's working for me now. Thank you for the very fast fix!