Personal tools
You are here: Home Documentation How-tos Archetypes using mysql and postgresql
Support

Get Help

Join our chat rooms or support forums if you have more specific questions.

Plone Training
Learn how to design, build, and deploy a website in Plone through one of the numerous Plone training sessions around the world.
Find Plone training…
 
Document Actions

Archetypes using mysql and postgresql

This How-to applies to: Plone 2.5.x
This How-to is intended for: Server Administrators, Site Administrators

A short tutorial about how to make an archetype that stores data in and reads data from a mysql or a postgresql table. It works on plone-2.5.2 on zope-2.9.5.
  1. Install MySQL-python and ZMySQLDA
    http://sourceforge.net/project/showfiles.php?group_id=22307
    Make a mysql-database connection in the zmi at your Plonesite's root (or somewhere else where Plone automatically finds it).
    Test the connection.
  2. Make a simple archetype. For the sake of simplicity, start with store/reading just one value: I call it testvalue in the example below. Install your product in the Products folder and in your plone site. Test that the archetype works. You may use ArgoUMl:
    http://plone.org/documentation/tutorial/anonymously-adding-custom-content-types-with-argouml-and-archgenxml/?searchterm=argouml
  3. In your archetype main script add:
    from Products.Archetypes.SQLStorage import MySQLSQLStorage
    In the fields that you want to store in mysql you add:
    storage=MySQLSQLStorage()
    Restart zope.
  4. Select the right connection. In the zmi, goto your Plone site root, and select "archetype_tool". Hit the Connections tab and ensure that your achetype uses the right mysqldatabase connection.
  5. Make the mysql table. Go to the mysqldatabase connection and select the test tab. There you create a table with exactly the same name as your archetype main script, except ".py". In my example, the table is called "insertNews". The table shall have three columns: UID, PARENTUID, testvalue. (The table may have additional columns, which have no corresponding field in your archetype. Such fields are left empty by your archetype - except autoincrement and timestamp fields.)
  6. Go to your plonesite and add your archetype. Now you should be able to store, read and update testvalue.

When your type works, you may change the base_view template (see the example below) to a template af your taste. In that case, you must go to the Plone site's root and click on portal_types. Then, click on your archetype (in the example, it is insertNews). There, and in the actions tab you may change base_view to your own template where this is appropriate.
 

See also: The old archetypes developer guide.

 

Postgresql

If you want to use Postgresql, you can do the same as described above with the following modifications:

Under point 1, you should use the product ZpsycopgDA for making connections to the database:

http://plone.org/documentation/how-to/postgresql-support-for-archetype-storage

 

 
Under point 3 you should use:

from Products.Archetypes.SQLStorage import PostSQLStorage

and

storage=PostSQLStorage()

Point 5 may be ignored since the archetype makes it itself.

Please, notice that the name of your archetype main script should be in lower case. Else you may get problems with your postgresql table.

Wysiwyg and default fields

Textfields may corrupt your Archetype if the field uses storage=MySQLSQLStorage(). Moreover, richwidgets seem neither to work in mysql nor postgresql so kupu wysiwyg is not a right forward option. If you want to use a wysiwig-editor, you may install the product epoz 

http://iungo.org/products/Epoz

 


You may find that a StringField with an EpozWidget and postgresql storage work properly. However,  for at least one reason epoz is not an optimal choice: When the user insert information in an archetype and makes an insert error, she may loose all information in the epoz-field.

There are problems with default values which do not turn up in MySQLSQLStrorage fields.

You can make an ugly work around for these weaknesses by not using storage=MySQLSQLStorage() in kupu and default fields. Then, you can customize base_view from the portal_skins/archetypes and call it: <yourType>_view.  In this file, you can store the values from the kupu and default fields in your sql database when the user comes from base_edit. Absolutely ugly, but it works.

Example with mysql storage

# File: insertnews.py
#
__author__ = """unknown <unknown>"""
__docformat__ = 'plaintext'

from AccessControl import ClassSecurityInfo
from Products.Archetypes.atapi import *
from Products.Insertnews.config import *
from Products.Archetypes.SQLStorage import MySQLSQLStorage

schema = Schema((
    StringField(
        name='testvalue',
        widget=StringWidget(
            label='Store this value in your mysql database',
            label_msgid='Insertnews_label_responsible',
            i18n_domain='Insertnews',
        ),
        required=True,
        storage=MySQLSQLStorage()
    ),
),
)

insertnews_schema = BaseSchema.copy() + \
    schema.copy()

class insertnews(BaseContent):
    """
    """
    security = ClassSecurityInfo()
    __implements__ = (getattr(BaseContent,'__implements__',()),)

    archetype_name = 'insertnews'
    meta_type = 'insertnews'
    portal_type = 'insertnews'
#    allowed_content_types = []
#    filter_content_types = 0
    global_allow = 1
    #content_icon = 'insertnews.gif'
    immediate_view = 'base_view'
    default_view = 'base_view'
    suppl_views = ()
    typeDescription = "insertnews"
    typeDescMsgId = 'description_edit_insertnews'

    _at_rename_after_creation = True

    schema = insertnews_schema

registerType(insertnews, PROJECTNAME)

 

 

by Flemming Bjerke last modified June 12, 2007 - 22:12 All content is copyright Plone Foundation and the individual contributors.

Epoz?

Posted by Geir Baekholt at March 15, 2007 - 08:48
I find it a bit strange to simply conclude that encoding breaks and the normal way of of defining richtext fields is unusable. Epoz is not commonly used. Kupu is far superior and is the standard for Plone. Epoz is not even maintained AFAIK.

I like this howto, but we cannot have documentation saying you have to switch wysiwyg-editors based on your storage.

I am guessing that setting the encoding right for the Mysql table will solve the problem.

encoding

Posted by Flemming Bjerke at March 16, 2007 - 09:42
Basically, I agree.

The problem is that no matter how I have tried (and I spend a lot of time on it, and I am not the only one), I haven't been able to make kupu work with mysql or postgresql. Let's say I make an archetype with a richwidget, and it works perfectly. If I change all other fields to post- og mysql storage, it still works. But, if I put post- or mysqlstorage on the richwidget textfield, the archetype crashes immediately. It doesn't matter whether I use utf-8 or latin-1. Concerning mysql, I have already described this on the archetype-users list, but I got no replies.

I have looked in the code to see, if I could pinpoint the problem. But, it would take me a least several days to get enough knowledge about plone and the sql-implementation to find exactly where the problem lies. That isn't possible for me.

Giving up kupu, I have made postgresql work properly with epoz, but I have met encoding problems with mysql. But, there is one problem with epoz (to my opinion the rest isn't decisive): if you make an error when you insert information into an archetype with epoz, e.g. you forget to fill in a mandatory field, then you loose all information inserted in epoz, and I KNOW that our users will be very, very annoyed.

My reason for mentioning all this, is that people should know that there are problems with wysiwyg, so that they need not wasting so much time as I have done. It is important that people know that there are problems.

So, I think it would be best to write:

It doesn't seem to be possible to have richwidgets (and thus kupu) store in mysql and postgresql. The only wysiwyg editor, I have been able to make work properly is epoz with postsql-storage. However, epoz has the drawback that the user looses information at fill in errors.

kupu and mysql

Posted by Flemming Bjerke at March 16, 2007 - 10:58
In mysql I ran: alter table insertnews set character utf8;
and under portalproperties I inserted uft-8. Then, I changed the field:

StringField(
name='short_dezcription',
allowable_content_types=('text/html','text/plain', 'text/structured',),
widget=RichWidget(
label='Short Description',
label_msgid='InsertNews_label_description',
i18n_domain='InsertNews',
),
default_output_type='text/html',
required=True,
storage=MySQLSQLStorage(),
),

and restarted. When I try to add try archetype object somewhere in portal, I get the error below (the same as I have encountered many times before with mysql and postgres as well).

I wish you were right about the mysql encoding.

Flemming

Module Products.PageTemplates.TALES, line 221, in evaluate
- URL: file:kupu/plone/kupu_plone_layer/kupu_wysiwyg_support.html
- Line 32, Column 5
- Expression: <PythonExpr here.contentUsesKupu(fname)>
- Names:
{'container': <PloneSite at /portaler/polforsk>,
'context': <insertnews at /portaler/polforsk/nytest/portal_factory/insertnews/insertnews.2007-03-16.0210721406 used for /portaler/polforsk/nytest>,
'default': <Products.PageTemplates.TALES.Default instance at 0x40f2e40c>,
'here': <insertnews at /portaler/polforsk/nytest/portal_factory/insertnews/insertnews.2007-03-16.0210721406 used for /portaler/polforsk/nytest>,
'loop': <Products.PageTemplates.TALES.SafeMapping object at 0x44b1b7ac>,
'modules': <Products.PageTemplates.ZRPythonExpr._SecureModuleImporter instance at 0x40f6122c>,
'nothing': None,
'options': {'args': (),
'state': <Products.CMFFormController.ControllerState.ControllerState object at 0x44cd596c>},
'repeat': <Products.PageTemplates.TALES.SafeMapping object at 0x44b1b7ac>,
'request': <HTTPRequest, URL=http://polforsk1.dk:8080/portaler/polforsk/nytest/portal_factory/insertnews/insertnews.2007-03-16.0210721406/base_edit>,
'root': <Application at >,
'template': <FSControllerPageTemplate at /portaler/polforsk/base_edit used for /portaler/polforsk/nytest/portal_factory/insertnews/insertnews.2007-03-16.0210721406>,
'traverse_subpath': [],
'user': <PloneUser 'bjerke'>}
Module Products.PageTemplates.ZRPythonExpr, line 47, in __call__
- __traceback_info__: here.contentUsesKupu(fname)
Module Python expression "here.contentUsesKupu(fname)", line 1, in <expression>
Module Shared.DC.Scripts.Bindings, line 311, in __call__
Module Shared.DC.Scripts.Bindings, line 348, in _bindAndExec
Module Products.PythonScripts.PythonScript, line 325, in _exec
Module None, line 28, in contentUsesKupu
- <PythonScript at /portaler/polforsk/contentUsesKupu used for /portaler/polforsk/nytest/portal_factory/insertnews/insertnews.2007-03-16.0210721406>
- Line 28
TypeError: len() of unsized object
g

My bugreport

Posted by Flemming Bjerke at March 16, 2007 - 11:37
http://dev.plone.org/archetypes/ticket/716

Error - PostgreSQLStorage()

Posted by Derek Broughton at November 2, 2007 - 13:12
The PostGre examples should use "PostgreSQLStorage" rather than "PostSQLStorage".

For any issues with the web site functionality, please file a ticket.

Please consult the policy on plone.org content if you want your content published on this site.

Servers and hosting by