cli icon indicating copy to clipboard operation
cli copied to clipboard

How Can I Specify Additional Attributes with "model:create"?

Open bookercodes opened this issue 9 years ago • 31 comments

Consider this command:

sequelize model:create --name User --attributes username:string

Running it will create a model with a username property and type attribute:

var User = sequelize.define('User', {                                                    
  username: DataTypes.STRING
});

Is it possible to specify additional attributes via the command-line? I would like to generate a model with the unique attribute:

// how do I generate this using model:create?
var User = sequelize.define('User', {                                                    
  username: {
    type: DataTypes.STRING,
    unique: true
  }
});

If not, please consider this to be a feature request.

bookercodes avatar Oct 31 '15 14:10 bookercodes

any updates on this? same issue going on here. would love to update my blog post -> http://mherman.org/blog/2015/10/22/node-postgres-sequelize/#.VkIr0a6rRE4

mjhea0 avatar Nov 10 '15 17:11 mjhea0

@mjhea0 I could never find a solution - I ended up writing my migrations by hand. It's not ideal but it's not so bad either.

bookercodes avatar Nov 10 '15 17:11 bookercodes

@sdepold any plans to add this capability? Thanks!

mjhea0 avatar Nov 11 '15 00:11 mjhea0

:wave:

I was originally planning to allow the following syntax:

sequelize model:create --name User --attributes "username:[type:string,unique:true]"

Opinion on that? Time is rare but I could try to dedicate some

sdepold avatar Nov 11 '15 05:11 sdepold

My ideal solution would work like this:

Command:

sequelize model:create --name User --attributes "email:[type:string, unique:true, allowNull: false, { validate: { isEmail: true } }]

Result:

email: {
      type: DataTypes.STRING,
      allowNull: false,
      validate: { 
        isEmail: true
      }    
    }

Thanks for your time, great package.

topleft avatar Nov 11 '15 13:11 topleft

I don't have time to look into it right this minute, but I seem to remember that sequelize-cmd has some command-line arguments for defining additional model attributes. Maybe it'll provide some inspiration.

bookercodes avatar Nov 11 '15 13:11 bookercodes

You can do it like this:

$ sequelize model:create --name TodoItem --attributes content:string,complete:boolean

donpinkus avatar Mar 09 '17 20:03 donpinkus

@topleft Is your solution already working?

ahelord avatar May 05 '17 15:05 ahelord

this is my version: Sequelize [Node: 6.6.0, CLI: 2.7.0, ORM: 4.3.2] and this command works for me: sequelize model:create --name= TodoItem --attributes=name:string,number:integer

thesnowgoose avatar Aug 02 '17 05:08 thesnowgoose

Any update on this? Haven't seen anything in docs sadly.

ChBernat avatar Apr 11 '18 14:04 ChBernat

Use model:generate like this: node_modules/.bin/sequelize model:generate --name User --attributes firstName:string,lastName:string,email:string

You can take a look at the documentation here: http://docs.sequelizejs.com/manual/tutorial/migrations.html#creating-first-model-and-migration-

CallMeSensei avatar May 15 '18 21:05 CallMeSensei

That is not the OP's question. They wanted to add different attributes to a specific column of the model, but the documentation only describes how to define the type of the column. In other words making the User model's firstName property unique or primaryKey or setting the defaultValue. Seems like this has to be done manually in the migration and model file.

k3zi avatar Jun 17 '18 00:06 k3zi

The lack of support basically makes model:create useless. I just came across this and what I am going to do is use sequelize-auto to generate my models. Once gen'd I will manually create the migration file. Not nice but it should do for now.

jonnydungeons avatar Nov 27 '18 22:11 jonnydungeons

That kind of feature would make the dev's work so much easier. At the moment I think the only solution is to generate the boilerplate then update the model and migration files.

mutaimwiti avatar Apr 01 '19 08:04 mutaimwiti

Hope this add additional attributes for variables will implement soon.

jerrychong25 avatar May 14 '19 05:05 jerrychong25

Manual seems to be the way to go until now.

alatras avatar Sep 11 '19 15:09 alatras

Can we reopen this issue or are there any open issues looking to implement this?

It seems like all that needs to happen to make this a reality is that we adapt the transformAttributes and formatAttributes function in helpers/model-helper.js a bit to generate a richer attribute object.

With the extra information in the attribute object we could expand the template in assets/models/model.js and get full attributes.

I like the syntax in https://github.com/sequelize/cli/issues/215#issuecomment-155786118, but I don't know why we'd need the curly braces around validate.

Helveg avatar Dec 17 '19 14:12 Helveg

Hey once I created the model with the model generator running the command model:create How can I import and use that new model?(e.g. how do I import and use it in other files for creating new rows in the table?) This is the code that was generated by the sequelize-api:

'use strict';
module.exports = (sequelize, DataTypes) => {
  const User = sequelize.define('user', {
    username: {type: DataTypes.STRING, allowNull: false},
    password: {type: DataTypes.STRING, allowNull: false},
    email: {type: DataTypes.STRING, allowNull: false},
    company_id: {type: DataTypes.STRING, allowNull: false}
  }, {});
  User.associate = function(models) {
    // associations can be defined here
  };
  return User;
};

In a new file I would like to do something like:

let User = require('user');
const jane = await User.create({ username: "Jane" , password: "Jane", email:"[email protected]", compay_id:"SomeCompany"});

omriShneor avatar Apr 08 '20 12:04 omriShneor

The lack of support basically makes model:create useless. I just came across this and what I am going to do is use sequelize-auto to generate my models. Once gen'd I will manually create the migration file. Not nice but it should do for now.

how do you create migrations for existing models?

deepakbhartii avatar Apr 27 '20 12:04 deepakbhartii

Curious as well in what @bookercodes said, still in the docs it doesn't explicitly say that you can or can't. Would be great as well when creating the model/s to also specify any relations with other models.

alexbujenita avatar Jun 30 '20 11:06 alexbujenita

👋

I was originally planning to allow the following syntax:

sequelize model:create --name User --attributes "username:[type:string,unique:true]"

Opinion on that? Time is rare but I could try to dedicate some

this makes perfect sense and very useful, and apparently it cant be done yet with the cli :/. I've been looking everywhere if its possible but cant find anything

Juan321654 avatar Jun 07 '21 16:06 Juan321654

Why is this closed? Is there any progress?

ralppppy avatar Aug 21 '21 04:08 ralppppy

gosh, been a minute since this was asked. anything?

riserperson avatar Jan 27 '22 01:01 riserperson

The bulk of our effort is currently on the main library but if someone wants to open a PR with an implementation of the proposed syntax, we'll review & merge it

ephys avatar Jan 27 '22 08:01 ephys

The lack of support basically makes model:create useless.

I'm new to sequelize and this is a pain point that I just run up against. I agree with this quote; it's a big enough pain point to make me reconsider using sequelize.

jonathan-chin avatar Feb 03 '22 17:02 jonathan-chin

I'm new to sequelize and this is a pain point that I just run up against.

Agreed. Without allowing attributes like allowNull=false, you end up writing these constraints twice: once in the model and again in the migration.

calteran avatar May 31 '22 07:05 calteran

Hi! Any news on this?

jero237 avatar Aug 18 '22 16:08 jero237

Hey Guys! Any update on this? Is anyone now able to add extra properties to the attributes using the sequelize-cli?

michaelAdewunmi avatar Oct 29 '22 16:10 michaelAdewunmi

It's 2013 and this still hasn't been added D:

I've been google searching and documentation reading for an hour hoping to find something about this but I guess it's just not implemented yet. It makes putting column properties kinda wonky because first you need to generate a migration file and then go and edit it manually which idealistically you're not really supposed to do as the cli command should be generating it exactly as you need it.

JamesAlphonse avatar Jun 19 '23 03:06 JamesAlphonse

Here to show my support for this kind of feature

YSHebron avatar Jul 01 '23 02:07 YSHebron