|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +title: Commom Pitfalls |
| 4 | +body_id: docs |
| 5 | +created_at: Sun Apr 01 21:13:13 -0700 2012 |
| 6 | +--- |
| 7 | + |
| 8 | +{{ page.title }} |
| 9 | +================ |
| 10 | + |
| 11 | +Below is a list of common problems that someone new to DataMapper will |
| 12 | +encounter, along with work-arounds or solutions if possible. |
| 13 | + |
| 14 | +Implicit String property length |
| 15 | +------------------------------- |
| 16 | + |
| 17 | +When declaring a String property, DataMapper will add an implicit length of 50 |
| 18 | +characters if nothing is explicitly declared. |
| 19 | + |
| 20 | +For example, the two class declarations have identical behaviour: |
| 21 | + |
| 22 | +{% highlight ruby linenos %} |
| 23 | +# with an implicit length |
| 24 | +class Post |
| 25 | + include DataMapper::Resource |
| 26 | + |
| 27 | + property :title, String |
| 28 | +end |
| 29 | +{% endhighlight %} |
| 30 | + |
| 31 | +{% highlight ruby linenos %} |
| 32 | +# with an explicit length |
| 33 | +class Post |
| 34 | + include DataMapper::Resource |
| 35 | + |
| 36 | + property :title, String, :length => 50 |
| 37 | +end |
| 38 | +{% endhighlight %} |
| 39 | + |
| 40 | +The reason for this default is that DataMapper needs to know the underlying |
| 41 | +column constraints in order to add validations from the property definitions. |
| 42 | +Databases will often choose their own arbitrary length constraints if one is not |
| 43 | +declared (often 255 chars). We choose something a bit more restricted as a |
| 44 | +default because we wanted to encourage peolpe to declare it explicitly in their |
| 45 | +model, rather than relying on DM or the DB choosing an implicit limit. |
0 commit comments