导致ts错误。类型““ long”'的参数不能分配给类型'TextDataTypeOptions'的参数

时间:2020-01-26 11:58:51

标签: node.js typescript sequelize.js

import { Sequelize, DataTypes, Model, BuildOptions } from 'sequelize';

...

client_data: {
    type: DataTypes.TEXT('long'),
    allowNull: false
}

我搜索了所有可以找到的东西,他们说这是正确的。

1 个答案:

答案 0 :(得分:0)

对于"sequelize": "^5.21.3"postgresql,简短回答:

应为DataTypes.TEXT({ length: 'long' })DataTypes.TEXT()方法的参数界面如下:

data-types.d.ts

export interface TextDataTypeOptions {
  length?: TextLength;
}

长答案,一个完整的工作示例:

index.ts

import { DataTypes, Model } from 'sequelize';
import { sequelize } from '../../db';

class SomeEntity extends Model {}
SomeEntity.init(
  {
    client_data: {
      type: DataTypes.TEXT({ length: 'long' }),
      allowNull: false,
    },
  },
  { sequelize, modelName: 'SomeEntities' },
);

(async function test() {
  try {
    await sequelize.sync({ force: true });
  } catch (error) {
    console.log(error);
  } finally {
    await sequelize.close();
  }
})();

执行上述代码后,使用\d[S+] NAME cli中的psqlSomeEntities关系的列上查找信息:

node-sequelize-examples=# \d "SomeEntities"
                             Table "public.SomeEntities"
   Column    |  Type   |                          Modifiers
-------------+---------+-------------------------------------------------------------
 id          | integer | not null default nextval('"SomeEntities_id_seq"'::regclass)
 client_data | text    | not null
Indexes:
    "SomeEntities_pkey" PRIMARY KEY, btree (id)

client_data列的数据类型为text

执行调试消息:

{ POSTGRES_HOST: '127.0.0.1',
  POSTGRES_PORT: '5430',
  POSTGRES_PASSWORD: 'testpass',
  POSTGRES_USER: 'testuser',
  POSTGRES_DB: 'node-sequelize-examples' }
Executing (default): DROP TABLE IF EXISTS "SomeEntities" CASCADE;
Executing (default): DROP TABLE IF EXISTS "SomeEntities" CASCADE;
(sequelize) Warning: PostgreSQL does not support TEXT with options. Plain `TEXT` will be used instead. 
>> Check: http://www.postgresql.org/docs/9.4/static/datatype.html
Executing (default): CREATE TABLE IF NOT EXISTS "SomeEntities" ("id"   SERIAL , "client_data" TEXT 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 = 'SomeEntities' GROUP BY i.relname, ix.indexrelid, ix.indisprimary, ix.indisunique, ix.indkey ORDER BY i.relname;