I created a customer model, then this is the model
then i tried to insert data into it sequelize command
after i sent the post request to add the data here is what I got sequelize error
the name of the model and table is customer (singular) but sequelize is looking for customers (plural).
Ok, the problem is if you want singular table name other than auto generated plural name, you should update Sequelize configuration.
You can checkout this question and its answer.
Related
I have defined db name in squelize constructor so that tables being generated by corresponding model files. Tables are successfully created in given db. I am confused with the schema which by default is public in models.So why to define schema in models if tables are properly generated in db in default postgresql server named PostgreSQL 15. if I change name of schema from public to some how ALGH it says schema doesnot exist. if it is referencing schema then what is the purpose of giving db name in sequelize constructor.
You can have multiple schemas in the same DB for different purposes like to divide sets of tables for different apps/services or by functionality.
You can indicate a schema in the Sequelize instance and it will be the default schema for all models that does not have an explicit schema OR you can indicate a schema for certain models only and other models will have the default public schema.
Of course, you need to create tables only after you decided and indicated all schemas (either in Sequelize instance and/or in certain models) you need otherwise you will get such errors you already mentioned in the post.
Ideally, for populate to work during queries in mongoose, both the schemas of that which is been populated and the calling schema needs to be imported in same file under same repo.
Now, I have a scenario where a schema called AuthorSchema was created by another internal micro-service in another repo which is pointing to the same database as my own service and I wish to populate it during my queries.
Is there a way to automatically decode the AuthorSchema from the author collection name after I've connected to the database?
E.g
let posts = await Post.find({})
.populate("author", 'first_name last_name _id').limit(10)
Where I have created postSchema in my own service but authorSchema was created by another micro-service in another repo called author-management-microservice and as such I have no access to that schema.
What I would want is something like:
let AuthorSchema = mongoose.connection.decodeSchemaFromCollectionName('author');
Such that I can modify my query to something like
let posts = await Post.find({})
.populate({path:"author", model:AuthorSchema, select:'first_name last_name _id'}).limit(10)
The reason I want something like this is because I have been battling with MissingSchemaError: Schema hasn't been registered for model "Author". whenever I try to populate author and I don't want to copy around duplicate schema files across different micro-services.
To get the schema from a registered Mongoose model, you need to access the schema specifically:
const AuthorSchema = require('mongoose').model('Author').schema;
Is there a way to automatically decode the AuthorSchema from the author collection name after I've connected to the database?
No - the schema is saved at the code level, not the database level. There is no way to know the schema just from connecting to the database.
I have a question in mongoDB. I have a user schema with different fields and methods. But I want to when deleting a user from this schema to place it in another schema for example deleted-users schema.
Should I create the deleted-users schema with the same exact fields but different name ?
Or is there a better way to this
I'm trying to do sequelize migrations on the models I created such as User.js, Blogs.js etc... What I've noticed is that I can add columns to Models like User.js and Blogs.js, but if I dont change the files in my models folder, the changes will be recognized in the database, but I wont be able to access the new columns when the server is running.
EG:
I add column permission: {type: Sequelize.STRING} to User.js.
I can't access this column because my original model file doesn't contain permission. Do I have to delete the models, and then create them using the sequelize-cli in order to get the migrations to also update the model files?
Your question is somewhat unclear.
In order to add a new column to a table using Sequelize migrations, you need to do the following steps:
Generate a new migration file.
Using queryInterface methods, make the changes to your tables in the migration file.
Execute your migrations.
Update your models with any new properties that are added.
Restart your server.
Keep in mind that migrations are done as an ALTERNATIVE to using the sequelize.sync() method. If you are using the sync() method, you cannot add columns to a table without using the { force: true } flag inside of sync(), which causes your tables to be dropped and data lost. Migrations are a process that requires a bit more attention to be paid, but they are a feasible long-term solution for a production database.
I have used ORMS such as EntityFramework, Waterline and Mongoose. I just started using Sequelize. I have created a sample model. Later I wanted to add another column to the table corresponding to the same model. When I edit the model, the new field doesn't seem to be reflecting in the table unless I drop the table. Am I supposed to use Migrations? I got confused after reading the documentation. Could someone help me out?
Yes, you should use migrations as if you edit a model in sequelize when you application starts it will only create the table if it doesn't exist. It does not check if the schema is different thus it doesn't update the schema of your table.
If I were you, I would install sequelize-cli and then run the sequelize init command. This sets up your project ready to use migrations.
Please note that we using sequelize migrations you have to explicitly define the primary key, updatedAt and createdAt columns otherwise they will not great created!
The docs are not too bad about migrations: http://sequelize.readthedocs.org/en/latest/docs/migrations/