Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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,

like image 574
Uri Barenholz Avatar asked May 03 '12 09:05

Uri Barenholz


People also ask

What are the rules for naming a table in mysql?

By default, MySQL encloses column names and table names in quotation marks. Table names can use any character that is allowed in a file name except for a period or a forward slash. Table names must be 32 characters or less because SAS does not truncate a longer name.

Should SQL table names be capitalized?

Only Use Lowercase Letters, Numbers, and Underscorestable. column pattern. Queries are harder to write if you use capital letters in table or column names. If everything is lowercase, no one has to remember if the users table is Users or users.

How do I print a table name in select query?

The syntax to get all table names with the help of SELECT statement. mysql> use test; Database changed mysql> SELECT Table_name as TablesName from information_schema. tables where table_schema = 'test'; Output with the name of the three tables.


1 Answers

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.

like image 121
Michael Snoyman Avatar answered Oct 20 '22 02:10

Michael Snoyman