the problem using sequence in postgresql via sqlalchemy,uh a patch file.

By admin, 7 四月, 2010, No Comment

Ok, I want use the customer sequence in the postgresql primary key.
so I define a sequence in the primary key column as default.
but It’s seem not work.

==================================== list 1

Python 2.6.4 (r264:75706, Feb 24 2010, 22:27:51)
[GCC 4.2.1 (Apple Inc. build 5646) (dot 1)] on darwin
Type “help”, “copyright”, “credits” or “license” for more information.
>>> import sqlalchemy
>>> print sqlalchemy.__version__
0.6beta3
>>> from sqlalchemy import create_engine
>>> engine = create_engine(’postgresql://postgres::@localhost/sqlalchemy’, echo=True)
>>> from sqlalchemy import Table, Column, Integer, String, MetaData
>>> from sqlalchemy import Sequence
>>> metadata = MetaData()
>>> users = Table(’users’, metadata,
…     Column(’id’, Integer, default=Sequence(’users_pk_seq’, start=100), primary_key=True),
…     Column(’name’, String),
…     Column(’fullname’, String),
… )
>>>
>>> metadata.create_all(engine)
2010-04-07 20:22:30,209 INFO sqlalchemy.engine.base.Engine.0x…ab50 select relname from pg_class c join pg_namespace n on n.oid=c.relnamespace where n.nspname=current_schema() and lower(relname)=%(name)s
2010-04-07 20:22:30,210 INFO sqlalchemy.engine.base.Engine.0x…ab50 {’name’: u’users’}
2010-04-07 20:22:30,219 INFO sqlalchemy.engine.base.Engine.0x…ab50 SELECT relname FROM pg_class c join pg_namespace n on n.oid=c.relnamespace where relkind=’S’ and n.nspname=current_schema() and lower(relname)=%(name)s
2010-04-07 20:22:30,220 INFO sqlalchemy.engine.base.Engine.0x…ab50 {’name’: u’users_pk_seq’}
2010-04-07 20:22:30,221 INFO sqlalchemy.engine.base.Engine.0x…ab50 CREATE SEQUENCE users_pk_seq START WITH 100      // the sequence is created
2010-04-07 20:22:30,221 INFO sqlalchemy.engine.base.Engine.0x…ab50 {}
2010-04-07 20:22:30,346 INFO sqlalchemy.engine.base.Engine.0x…ab50 COMMIT
2010-04-07 20:22:30,348 INFO sqlalchemy.engine.base.Engine.0x…ab50
CREATE TABLE users (
id INTEGER NOT NULL,       // but there is not use the ‘users_pk_seq’ sequence.this column hope (id INTEGER DEFAULT nextval(users_pk_seq) NOT NULL,)
name VARCHAR,
fullname VARCHAR,
PRIMARY KEY (id)
)

2010-04-07 20:22:30,348 INFO sqlalchemy.engine.base.Engine.0x…ab50 {}
2010-04-07 20:22:30,410 INFO sqlalchemy.engine.base.Engine.0x…ab50 COMMIT
>>>

==================================== list 1

the sqlalchemy schema

==================================== list 2

sqlalchemy=# \d users;
Table “public.users”
Column  |       Type        | Modifiers
———-+——————-+———–
id       | integer           | not null
name     | character varying |
fullname | character varying |
Indexes:
“users_pkey” PRIMARY KEY, btree (id)

sqlalchemy=#

==================================== list 2

so I hack a file in the sqlalchemy
this is the diff file.
OK, if you have another better way,I will thank you if you tell me.

—–

the new version

==================================== list 3

>>> metadata.create_all(engine)
2010-04-07 20:49:12,602 INFO sqlalchemy.engine.base.Engine.0x…ab90 select version()
2010-04-07 20:49:12,602 INFO sqlalchemy.engine.base.Engine.0x…ab90 {}
2010-04-07 20:49:12,604 INFO sqlalchemy.engine.base.Engine.0x…ab90 select current_schema()
2010-04-07 20:49:12,604 INFO sqlalchemy.engine.base.Engine.0x…ab90 {}
2010-04-07 20:49:12,607 INFO sqlalchemy.engine.base.Engine.0x…ab90 select relname from pg_class c join pg_namespace n on n.oid=c.relnamespace where n.nspname=current_schema() and lower(relname)=%(name)s
2010-04-07 20:49:12,607 INFO sqlalchemy.engine.base.Engine.0x…ab90 {’name’: u’users’}
2010-04-07 20:49:12,610 INFO sqlalchemy.engine.base.Engine.0x…ab90 SELECT relname FROM pg_class c join pg_namespace n on n.oid=c.relnamespace where relkind=’S’ and n.nspname=current_schema() and lower(relname)=%(name)s
2010-04-07 20:49:12,610 INFO sqlalchemy.engine.base.Engine.0x…ab90 {’name’: u’users_pk_seq’}
2010-04-07 20:49:12,611 INFO sqlalchemy.engine.base.Engine.0x…ab90
CREATE TABLE users (
id INTEGER DEFAULT nextval(’users_pk_seq’) NOT NULL,   // bingo.
name VARCHAR,
fullname VARCHAR,
PRIMARY KEY (id)
)

2010-04-07 20:49:12,612 INFO sqlalchemy.engine.base.Engine.0x…ab90 {}
2010-04-07 20:49:12,694 INFO sqlalchemy.engine.base.Engine.0x…ab90 COMMIT

==================================== list 3

in postgresql

==================================== list 4

sqlalchemy=# \d users;
Table “public.users”
Column  |       Type        |                     Modifiers
———-+——————-+—————————————————-
id       | integer           | not null default nextval(’users_pk_seq’::regclass)
name     | character varying |
fullname | character varying |
Indexes:
“users_pkey” PRIMARY KEY, btree (id)

sqlalchemy=#

==================================== list 4

here is the patch file

http://my.unix-center.net/~WeiZhicheng/wp-content/sa_sequence.patch

iPhone oauth object-c lib OAuthConsumer patch

By admin, 10 三月, 2010, No Comment

I’m developer some oauth client application for iPhone

and i use the OAuthConsumer It’s has some trouble

and i fix it,

this is patch file.

http://my.unix-center.net/~WeiZhicheng/wp-content/OAuthConsumer.patch

I got a Mac

By admin, 27 二月, 2010, 2 Comments

pictures…

SQLAlchemy 0.6 UnicodeDecodeError Bug Patch

By admin, 13 一月, 2010, No Comment

I’m using sqlalchemy 0.6 and mysql in my project,

but some times I got UnicodeDecodeError

and I check the sqlalchemy souce code and

I found why has this error,

It’s because when i insert a String(include Chinese UTF-8)

if the String byte not same with this field byte,maybe the

last one Chinese Char not insert complete.(BTW,Chinese is 3 bytes in UTF-8)

so if i query this field,utf-8 codec throw an error to me.

and write a patch to the sqlalchemy,It’s can fix this problem.

but It’s not perfect,this issue is the mysql not sqlalchemy.

so,if you write software like this,please careful process Chinese code and all non-ansi

code

patch download url:

http://my.unix-center.net/~WeiZhicheng/sqlalchemy.diff

for changeset:4721 in mericural repo

It’s My fault I don’t know the SQLAlchemy need set db connect charset

this is solution

engine = create_engine(’mysql://root:@localhost/dev?charset=utf8‘)

Sorry for All

请大家帮助一位Linux程序员的女儿

By admin, 5 一月, 2010, No Comment

这位小女孩叫怡帆,目前靠氧气机呼吸维持生命,要治愈小怡帆病,只能进行儿童肺移植手术。而这类手术在国内尚未有成功先例,而美国德克萨斯儿童医院 则经验丰富。潘俊廷和妻子周萍已经卖掉了房子,然而依然不够手术所需的50万美金(约合341万元人民币)的手术费用,对潘俊廷来说无异于天文数字。

潘俊廷是一位非常优秀的Linux程序员,他担任中国Linux论坛的版本,此网站会员捐赠42笔共42542.79元,目前来自各方的捐款合计达到了35万无,尽管如此,离手术费用还是相当的大,希望各位朋友一起帮助这位小女孩渡过难关。

帮助怡帆的官方网站,那里有详细的病情说明,并提供了捐款信息。

SQLAlchemy declarative ManyToMany,No need define Table(…)

By admin, 9 十一月, 2009, 1 Comment

i try sqlalchemy in this day,
and i have some trouble,it’s in my table,i use the declarative,define class,table,mapper together,
but when i have an ManyToMany relation,i must define a Table,It’s in offical document,
i don’t want define it,just use class,it’s good for my define database table,so i find a way,
Ok, that’s it I found

Mac OSX 安装cmemcache

By admin, 26 十月, 2009, No Comment

工作环境从Linux转到Mac上,但对Mac不是很熟悉,
在安装python的memcached扩展cmemcache时卡住了。

...

django url中文参数问题及解决方法

By admin, 28 九月, 2009, 1 Comment

如果是默认django设置如果在url中加入中文参数
django是不能解析的.解决办法有两个

在django中创建cron任务

By admin, 24 九月, 2009, No Comment

django默认好像不支持cron任务,google code中有一个针对django

的库 (google搜索django cron).

不过对于这个小功能我还是喜欢自己动手.

在Django中使用Mako模板

By admin, 24 九月, 2009, 4 Comments

不喜欢django的模板系统,

Mako一直很不错,可以在模板中直接写Python代码,

如果在django中使用Mako模板可以用下面的方法