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

Add support for NOW() as a default value for timestamp columns #154

Open
kilnerm opened this issue Sep 5, 2018 · 1 comment
Open

Add support for NOW() as a default value for timestamp columns #154

kilnerm opened this issue Sep 5, 2018 · 1 comment

Comments

@kilnerm
Copy link
Contributor

kilnerm commented Sep 5, 2018

Having the ability to use NOW() as a default value would allow users to implement an automatic created at column in their table definitions.

This work could be scoped with issue #117,

@taylorwmitchell
Copy link

taylorwmitchell commented Jul 18, 2019

This may be helpful as a temporary workaround. Note this is using a timestamp data type / MySQL.

/**
 When creating a Column, a default value can be specified. If a string
 is provided, the MySQLConnection.packType() method wraps the value
 with "\'". This makes sense if you are specifying a default value
 that is constant, but means the generated query for a current_timestamp
 default value doesn't work (MySQL wants current_timestamp, not \"current_timestamp\")
 
 The workaround is to provide a String(describing: _) value, which is done here.
 */
public enum DefaultValues {
    public static let currentTimestamp = CurrentTimestamp()
    public static let updatingTimestamp = UpdatingCurrentTimestamp()
}

public class CurrentTimestamp: CustomStringConvertible {
    public var description = "CURRENT_TIMESTAMP"
}

public class UpdatingCurrentTimestamp: CustomStringConvertible {
    public var description = "CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"
}

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

No branches or pull requests

2 participants