Adding Swift Vapor Fluent Foreign Key Constraint with PostgreSQL

When I run this steam running prepare command line , I get correct tables, but no foreign key constraints are added.

I have a Theme class and a Question class:

Subject: Name, ID

Question: title, I would, theme_id

This is the database preparation function in my Question class:

static func prepare(_ database: Database) throws {

        try database.create("questions") { questions in
            questions.id()
            questions.string("title")
            questions.parent(Theme.self, optional: false)
        }


    }

      

+3


source to share


2 answers


Fluent 2 adds foreign key constraints.



https://docs.vapor.codes/2.0/fluent/database/#foreign-keys

+1


source


In addition, during preparation, you can add an original SQL query that can get you out of most situations. For example, you can add this extra row to create a unique constraint across multiple columns.

questions.raw("UNIQUE (pararent_id, title)")

      



But it serves many purposes ...

0


source







All Articles