Closed kimdv closed 4 years ago
Unrelated to the feature request: be aware that adding a non-null column to a Postgres will rewrite the entire table (and lock it for the duration of that process). That can potentially take very long and bring your service down.
On 28. Jul 2018, at 09:11, Kim de Vos notifications@github.com wrote:
Right now the documentation don't say anything about the tui adding default value when adding mass to Galaxy Ass mass not is optional this will fail.
Is there a way to add default value without writing a raw query?
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.
Agreed that SchemaBuilder
related operations could use some example documentation in this regard. Short answer is "yes"
builder.field(for: \.mass, type: .int, .notNull, .default(0))
https://api.vapor.codes/sql/latest/SQL/Protocols/SQLColumnConstraint.html https://api.vapor.codes/sql/latest/SQL/Protocols/SQLColumnBuilder.html
Closing in favor of https://github.com/vapor/fluent-kit/issues/324, thanks.
For now, the best way to do this in Fluent 4 is with custom SQL: https://docs.vapor.codes/4.0/fluent/advanced/#sql
@tanner0101 Pardon me if this is not the place to ask, but could you give an example on how to add a .required
property in a Migration func prepare(on database: Database) -> EventLoopFuture<Void>
method when the database is not empty?
Thank you!
@xiao99xiao proceed with caution but this is how I managed to set a required field for a Postgres DB:
struct MakeEnduserDateOfBirthRequiredMigration: Migration {
func prepare(on database: Database) -> EventLoopFuture<Void> {
if let db = database as? PostgresDatabase {
return db.simpleQuery("""
ALTER TABLE "public"."endusers" ALTER COLUMN "date_of_birth" SET NOT NULL;
""")
.flatMap { _ in
return database.eventLoop.future()
}
} else {
fatalError()
}
}
func revert(on database: Database) -> EventLoopFuture<Void> {
if let db = database as? PostgresDatabase {
return db.simpleQuery("""
ALTER TABLE "public"."endusers" ALTER COLUMN "date_of_birth" DROP NOT NULL;
""")
.flatMap { _ in
return database.eventLoop.future()
}
} else {
fatalError()
}
}
}
@zastrozzi Great work! Many thanks!!
Right now the documentation don't say anything about adding default value when adding
mass
toGalaxy
Assmass
not is optional this will fail.Is there a way to add default value without writing a raw query?