cli
cli copied to clipboard
How Can I Specify Additional Attributes with "model:create"?
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.
👋
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
Why is this closed? Is there any progress?
gosh, been a minute since this was asked. anything?
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
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.
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.
Hi! Any news on this?
Hey Guys! Any update on this? Is anyone now able to add extra properties to the attributes using the sequelize-cli?
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.
Here to show my support for this kind of feature