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

set default value for created_at in code and not in schema #3627

Closed
christophmayrhofer opened this issue Feb 12, 2019 · 2 comments
Closed

set default value for created_at in code and not in schema #3627

christophmayrhofer opened this issue Feb 12, 2019 · 2 comments

Comments

@christophmayrhofer
Copy link

Issue type:

[x] question
[ ] bug report
[ ] feature request
[ ] documentation issue

Database system/driver:

[ ] cordova
[ ] mongodb
[ ] mssql
[x] mysql / mariadb
[ ] oracle
[ ] postgres
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo

TypeORM version:

[x] latest
[ ] @next
[ ] 0.x.x (or put your version here)

I want to automatically populate the created_at and updated_at columns.

    import {BaseEntity, CreateDateColumn, PrimaryGeneratedColumn, UpdateDateColumn} from "typeorm"
    import {Moment} from "moment"
    import {momentTransformer} from "../helpers/transformers"
    
    export default abstract class Model extends BaseEntity {
        @PrimaryGeneratedColumn()
        id: number
    
        @CreateDateColumn({type: "timestamp", transformer: momentTransformer})
        created_at: Moment
    
        @UpdateDateColumn({type: "timestamp", transformer: momentTransformer})
        updated_at: Moment
    }

How can I set a default value (current time) that is used for the sql query without altering the DB schema. I,e. I want the code to set the value and not the DB. The code above results in the following query:

INSERT INTO someTable(id, created_at, updated_at) VALUES (DEFAULT, DEFAULT, DEFAULT)

@vlapo
Copy link
Contributor

vlapo commented Feb 14, 2019

Do you want provide data for UpdateDateColumn and CreateDateColumn in your code instead of DB? Here maybe similar issue #3271. Please check it.

@Kononnable
Copy link
Contributor

Closing as no response from the author.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants