2

I'm trying to define my database structure, but I can't figure out how to set the default value of a column to NULL. This is what I want it to produce:

CREATE TABLE test (
    content text DEFAULT(NULL),
);

I have the following code

let content = Expression<String?>("content")

try self.db.run(test.create { t in
    t.column(content, defaultValue: nil)
})

But defaultValue: nil obviously does not work. What do I set instead?

oelna
  • 2,210
  • 3
  • 22
  • 40

1 Answers1

8

Unless you create the column in the table with the NOT NULL qualifier, the column's default value is NULL. You don't need to do anything special to get the behavior you want.

CREATE TABLE test {
    content TEXT
};

Create the table that way. When you insert rows into that table, if you don't provide a specific value for the content column, the row will be added with no value (NULL) in that column.

rmaddy
  • 314,917
  • 42
  • 532
  • 579