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.
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 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.
@sdepold any plans to add this capability? Thanks!
: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
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.
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.
You can do it like this:
$ sequelize model:create --name TodoItem --attributes content:string,complete:boolean
@topleft Is your solution already working?
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
Any update on this? Haven't seen anything in docs sadly.
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-
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.
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.
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.
Hope this add additional attributes for variables will implement soon.
Manual seems to be the way to go until now.
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.
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"});
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?
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.
Most helpful comment
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
uniqueorprimaryKeyor setting thedefaultValue. Seems like this has to be done manually in the migration and model file.