Reputation: 3
import { Sequelize, DataTypes, Model, BuildOptions } from 'sequelize';
...
client_data: {
type: DataTypes.TEXT('long'),
allowNull: false
}
I searched everything I can find and they say this is right.
Upvotes: 0
Views: 1358
Reputation: 102207
For "sequelize": "^5.21.3"
and postgresql
, short answer:
It should be DataTypes.TEXT({ length: 'long' })
; The parameter interface of the DataTypes.TEXT()
method is below:
data-types.d.ts
:
export interface TextDataTypeOptions {
length?: TextLength;
}
Long answer, a completed working example:
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();
}
})();
After executing the above code, using \d[S+] NAME
of psql
cli find the information on columns of SomeEntities
relation:
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)
The data type of client_data
column is text
as exepcted.
The execution debug message:
{ 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;
Upvotes: 1