Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sequelize create in the tables a column called "true" #12551

Closed
jbarretodev opened this issue Jul 24, 2020 · 3 comments · Fixed by #12580
Closed

Sequelize create in the tables a column called "true" #12551

jbarretodev opened this issue Jul 24, 2020 · 3 comments · Fixed by #12580
Labels
status: understood For issues. Applied when the issue is understood / reproducible. type: bug

Comments

@jbarretodev
Copy link

jbarretodev commented Jul 24, 2020

Issue Description

i dont know why! but sequelize is creating a column called "true".
i need help with this please!

here my codes:

`import { mainDBModelOption } from '..'
import {Model, DATE, INTEGER, STRING} from "sequelize";
import Sequelize from "sequelize";
import { ContactCreate, Contact_attributes } from "../../@types/contact_interface";

class Contact extends Model <Contact_attributes,ContactCreate> implements Contact_attributes{
public id!:number
public user!: number
public email!:string
public name!:string
public firstName!:string
public lastName!:string
public phoneNumber!:string
public companyName!:string
public website!:string
public address1!:string
public address2!:string
public postalCode!:string
public state!:string
public city!:string
public country!:string
public createdAt!:Date
public updatedAt!:Date
public deletedAt!:Date
}

Contact.init({
id: {
type: INTEGER,
primaryKey: true,
autoIncrement: true
},
user: {
type: STRING
},
email: {
type: STRING
},
name:{
type:STRING
},
firstName: {
type: STRING
},
lastName: {
type: STRING
},
phoneNumber: {
type: STRING
},
companyName: {
type: STRING
},
website: {
type: STRING
},
address1: {
type: STRING
},
address2: {
type: STRING
},
postalCode: {
type: STRING
},
state: {
type: STRING
},
city: {
type: STRING
},
country: {
type: STRING,
},
createdAt: {
type: DATE,
defaultValue: Sequelize.NOW
},
updatedAt: {
type: DATE,
defaultValue: null
},
deletedAt: {
type: DATE,
defaultValue: null
}

},mainDBModelOption('contacts'));

export default Contact`

`
import { Sequelize, InitOptions } from 'sequelize'
import database from './database.config'

/**

  • Sequelize configuration
    */
    const mainDB = new Sequelize(
    database.database,
    database.username,
    database.password,
    {
    host: database.host,
    port: database.port,
    dialect: database.dialect,
    define: {
    underscored: true,
    timestamps: true,
    createdAt: true,
    updatedAt: true,
    deletedAt: true,
    paranoid: true,
    }
    }
    )

export default mainDB
/**

  • Model options factory
  • @param db
    /
    export const modelOptionsFactory = (db: Sequelize) => (
    modelName: string
    ): InitOptions => {
    return {
    sequelize: db,
    modelName: modelName
    }
    }
    /
    *
  • Main Db models options
    /
    export const mainDBModelOption = modelOptionsFactory(mainDB)
    /
    *
  • Connection factory to db
  • @param db
    */
    export const connectFactory = (db: Sequelize): (() => Promise) => {
    let connected = false
    return async (): Promise => {
    if (connected) return connected
    await db.authenticate()
    await db.sync()
    connected = !connected
    return connected
    }
    }
    `
    Screenshot from 2020-07-24 16-45-37
@jbarretodev jbarretodev changed the title Sequelize create in a table a column called "true" Sequelize create in the tables a column called "true" Jul 24, 2020
@vishal-sood
Copy link
Contributor

vishal-sood commented Jul 28, 2020

@jbarretodev I faced the same issue & went to dig into the code to find a reason for it. So, the reason for this can be found here.

The model class in its init method has a strict equality check (===) against false while setting up timestamp related column names. So when you configure sequelize as follows, the equality against false fails and ends up creating a column named true.

...
define: {
    underscored: true,
    timestamps: true,
    createdAt: true,
    updatedAt: true,
    deletedAt: true,
    paranoid: true,
}
...

So for now, you can solve the problem by skipping the columns you do want (don't set them to true).

@papb I have 2 suggestions to fix this issue and make the configuration of timestamps work as (intuitively) expected.

  1. The check could be for the value of createdAt, updatedAt and deletedAt to be of type string. This way any string value will be used to rename the column. boolean values could be used to toggle the columns with default names. And, all other values will be discarded.
  2. Add a check for the value to be true. This way the functionality will work as it is working now, but true will not lead to creation of a column named true.

I can go ahead and open a PR for any of these solutions, just let me know which one will you prefer!

@jbarretodev
Copy link
Author

@vishal-sood Man its work for me, thanks a lot! But its something strange that! someone must to fix that!

@papb
Copy link
Member

papb commented Jul 30, 2020

@vishal-sood Fantastic! Thank you so much for taking the time to help!! Nice find! Indeed true should be understood as equivalent of simply wanting the default name. A PR with your first option would be excellent. However, instead of ignoring non-boolean, non-strings, I prefer you throw a TypeError.

Thank you!!

@papb papb reopened this Jul 30, 2020
@papb papb added status: understood For issues. Applied when the issue is understood / reproducible. type: bug labels Jul 30, 2020
vishal-sood added a commit to vishal-sood/sequelize that referenced this issue Aug 1, 2020
Updated Model.init method to accept only string and
boolean values. Also, prevent a column named `true` from
being created on the model whenever any timestamp fields
(viz. 'createdAt', 'updatedAt', 'deletedAt') is set to `true`

Closes sequelize#12551
vishal-sood added a commit to vishal-sood/sequelize that referenced this issue Aug 1, 2020
Updated Model.init method to accept only string and boolean values. Also, prevent a column
named `true` from being created on the model whenever any timestamp fields (viz. 'createdAt',
'updatedAt', 'deletedAt') is set to `true`

Closes sequelize#12551
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: understood For issues. Applied when the issue is understood / reproducible. type: bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants