This is really odd, and I really have no idea how to fix it.I've been playing around with Sequelize on a couple "practice" projects, learning it so that I can use it on a side project of mine.I created one project called sequelize-playground, (that was also the name of the database and database user) and ended up deleting it because of some off behavior. Essentially, the behavior that I experienced was that the Sequelize instance in my application was adding data somewhere I couldn't find. Basically, following this exact tutorial (https://sequelize.org/master/manual/migrations.html#creating-first-model--and-migration-) I created a model and migration, seeded the table with a sample entry, and everything was fine. I then modified the model, adding a password column (remember this for later), created a migration to add the column, and nothing changed in my database. However, when I attempted to use something like .findOrCreate, I was getting errors such as null value in column "password" violates not-null constraint, however when I used psql to describe the table, nothing had changed. There was no password field.So I thought it was weird, but ended up deleted the entire project, DB, everything. I created a NEW folder called seqpg, a new database called seqpg, and a new database superuser role called seqpg.I followed the tutorial above once again (https://sequelize.org/master/manual/migrations.html#creating-first-model--and-migration-), and now when I try to seed the data, I get an error that the password field is not allowed to be null.Image of error: https://imgur.com/sqWSHCHError:(base) ➜ seqpg node_modules/.bin/sequelize db:seed:all Sequelize CLI [Node: 11.6.0, CLI: 5.5.1, ORM: 5.18.4] Loaded configuration file "config/config.js". Using environment "development". == 20190917223253-demo-user: migrating ======= ERROR: null value in column "password" violates not-null constraint If I run psql and then \l, I ONLY see the seqpg database.my config.js file:const fs = require('fs'); module.exports = { development: { username: process.env.DB_USERNAME, password: process.env.DB_PASSWORD, database: process.env.DB_NAME, host: process.env.DB_HOSTNAME, dialect: 'postgres' }, production: { username: process.env.DB_USERNAME, password: process.env.DB_PASSWORD, database: process.env.DB_NAME, host: process.env.DB_HOSTNAME, dialect: 'postgres', } }; my .env file:DB_USERNAME='seqpg' DB_PASSWORD=null DB_NAME='seqpg' DB_HOSTNAME="127.0.0.1" my .sequelizerc file:const path = require('path'); module.exports = { 'config': path.resolve('config', 'config.js') } my model/user.js model:'use strict'; module.exports = (sequelize, DataTypes) => { const User = sequelize.define('User', { firstName: DataTypes.STRING, lastName: DataTypes.STRING, email: DataTypes.STRING }, {}); User.associate = function(models) { // associations can be defined here }; return User; }; my initial create-user migration:'use strict'; module.exports = { up: (queryInterface, Sequelize) => { return queryInterface.createTable('Users', { id: { allowNull: false, autoIncrement: true, primaryKey: true, type: Sequelize.INTEGER }, firstName: { type: Sequelize.STRING }, lastName: { type: Sequelize.STRING }, email: { type: Sequelize.STRING }, createdAt: { allowNull: false, type: Sequelize.DATE }, updatedAt: { allowNull: false, type: Sequelize.DATE } }); }, down: (queryInterface, Sequelize) => { return queryInterface.dropTable('Users'); } }; my seed file for sample data'use strict'; module.exports = { up: (queryInterface, Sequelize) => { return queryInterface.bulkInsert('Users', [{ firstName: 'John', lastName: 'Doe', email: 'demo@demo.com', createdAt: new Date(), updatedAt: new Date() }], {}); }, down: (queryInterface, Sequelize) => { return queryInterface.bulkDelete('Users', null, {}); } }; my package.json file:{ "name": "seqpg", "version": "1.0.0", "description": "", "main": "server.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC", "dependencies": { "dotenv": "^8.1.0", "express": "^4.17.1", "pg": "^7.12.1", "pg-hstore": "^2.3.3", "sequelize": "^5.18.4", "sequelize-cli": "^5.5.1" } } Again, I followed the tutorial linked above verbatim. The ONLY thing I did differently was change the config.json to config.js, add the .sequelizerc file, and of course add the migration to add the password field in the first project.I literally can't use sequelize because it's entering data into a database I've deleted (or at least referencing fields from a table in a database I deleted).Does anybody have any idea how I could fix this?Happy to provide more code or even a github link if needed.
Submitted September 18, 2019 at 12:01AM by wipedingold
No comments:
Post a Comment