What are the rules of mapping from PersistEntity/PersistField to column and table names in the DB

haskell, persistent, yesod

Solution

I'm not an authority on the MySQL backend, but IIRC (and based on the code), you can control the maximum length by using the `maxlen=...` attribute. Similarly, you can have direct control of the name the field will have in the database by using the `sql=...` attribute. So, for example, the following might work:

Person sql=people
    name Text sql=full_name maxlen=40
    age Int

I also agree that you should disable the automigration code if you're dealing with a pre-existing schema.

Problem

I need to work with an existing (MySql) db, where the names of tables and columns are already defined. If I understand the documentation properly (and I didn't find good documentation on this subject, so links will be highly appreciated), table names are related to PersistIdentity, and must therefore begin with a capital letter (which is not the case I'm facing). Column names, however, are automatically un-capitalized (at least that's what is implied in the Yesod book, Persistent chapter, in the code snippet describing the code automatically generated from declarations), so columns in the DB must begin with a lowercase letter. Is the description above indeed true? Can I control specifically the mapping of tables to identities and columns to fields? If not then what are the rules automatically applied for the naming? What names are therefore forbidden? Also, one of the fields is a VARCHAR(30). How can I communicate that to Persistent? It currently complains (through yesod devel) that: errMessage = "BLOB/TEXT column 'my_field' used in key specification without a key length"} Which is the result of auto-migration (which I probably should disable anyway). However, if I do want to declare a bounded VARCHAR field - can I do that through Persistent and its auto-migration tool? Thanks,

Original source