[Ur] (minor) hidden table constraints

Adam Chlipala adamc at csail.mit.edu
Thu Oct 17 09:26:29 EDT 2013


On 10/17/2013 08:32 AM, Sergey Mironov wrote:
> The scheme is following:
>
>
> (* Template.ur; page table is 'private' *)
> table page : { Id : int, MenuText : string }
>    PRIMARY KEY Id
> table product : { Id : int, Caption : string, Slogan : string, Logo :
> string, PageId : int }
>    PRIMARY KEY Id
>    CONSTRAINT PageLink FOREIGN KEY PageId REFERENCES page(Id)
>
> (* Template.urs declares only 'public' table, with no constraints *)
> table product : { Id : int, Caption : string, Slogan : string, Logo :
> string, PageId : int }
>
> (* App.ur *)
> val product = Template.product (* looks like table declarations don't
> like dots in names. *)
>    

Re: not liking dots, that's only the parsing sugar, which could 
potentially be extended; though your approach here also seems reasonable 
to me.

> table featuret : { Id : int, Content: string, ProductId : int }
>    PRIMARY KEY Id,
>    CONSTRAINT ProductLink FOREIGN KEY ProductId REFERENCES product(Id)
>
> The compiler doesn't allow this code.
>    

There's a good reason the compiler doesn't like that code: the signature 
of [Template] isn't enough to guarantee that you've written a valid SQL 
table definition here!  Foreign keys are only allowed to foreign fields 
that are actually _keys_, and Ur/Web uses types to track the existence 
of keys.

You'll need at least to indicate in Template.urs that [Id] is a key of 
[product].  Currently that requires either (1) abandoning the [table] 
syntactic sugar or (2) listing _all_ the constraints in Template.urs.

With (1), you can do (in Template.urs):
     con product_constraints :: {{Unit}}
     constraint [Pkey] ~ product_constraints
     val product : sql_table [Id = int, Caption = string, Slogan = 
string, Logo = string, PageId = int]
                             ([Pkey = [Id]] ++ product_constraints)

[Actually, I had to add a small improvement to type inference to get the 
example to work with only this change.  You can pull the patch from the 
public repo.]

It might be worth extending the [table] syntactic sugar to do something 
like the above automatically; maybe I'll try it later.



More information about the Ur mailing list