remove duplicates - DISTINCT constraint

1,083 views
Skip to first unread message

Disrupt07

unread,
Apr 3, 2007, 4:58:23 AM4/3/07
to sqlalchemy
I need to populate a table with author names
for example the table name is "author" and it has a field "authorname"
of type text. "authorname" has the following values:
W. Shakespear
J. Smith
W. Shakespear
R. Williams
K. Winslet
... and so on.

Then I want SQLAlchemy to remove the duplicates, so that "W.
Shakespear" will be saved only once.

How can I achieve this using SQLAlchemy?
Thanks

Glauco

unread,
Apr 3, 2007, 5:09:29 AM4/3/07
to sqlal...@googlegroups.com
Disrupt07 ha scritto:
Primary Key or unique constraint do this...


anyway if these names are stored in a list or something similar a
sequence...
firt of all put it in a <type 'set'> ...so automatically your problem is
gone..

In [21]: set( [ 'W. Shakespear', 'J. Smith', 'R. William', 'W.
Shakespear', 'J. Smith'])
Out[21]: set(['W. Shakespear', 'R. William', 'J. Smith'])


Glauco

--
+------------------------------------------------------------+
Glauco Uri - Programmatore
glauco(at)allevatori.com

Sfera Carta Software® in...@sferacarta.com
Via Bazzanese,69 Casalecchio di Reno(BO) - Tel. 051591054
+------------------------------------------------------------+


Disrupt07

unread,
Apr 3, 2007, 5:12:50 AM4/3/07
to sqlalchemy
Thanks. But what do you mean by "a <type 'set'>" ? Is this a
functionality of SQLAlchemy or of a list or some programming language?

Please explain how to do it in SQLAlchemy because I want to populate
the table initially with all of the data, then I want SQLAlchemy to
save only unique distinct values.

Glauco

unread,
Apr 3, 2007, 5:22:03 AM4/3/07
to sqlal...@googlegroups.com
Disrupt07 ha scritto:

> Thanks. But what do you mean by "a <type 'set'>" ? Is this a
> functionality of SQLAlchemy or of a list or some programming language?
>
>

What's your data sources ? a python list? a text file? another DB?

You can do a sequence of insert into with a unique contraint over that
field so duplicate records will not be stored...


the set otherwise is only usable if you have a list (from a turbo Gear
Form form for example )


a = Authors()
my_authors= [ 'W. Shakespear', 'J. Smith', 'R. William', 'W.
Shakespear', 'J. Smith']

for x in set( my_authors )
a.new( x )


> Please explain how to do it in SQLAlchemy because I want to populate
> the table initially with all of the data, then I want SQLAlchemy to
> save only unique distinct values.
>
>

Glauco

--
+------------------------------------------------------------+
Glauco Uri - Programmatore
glauco(at)allevatori.com

Sfera Carta SoftwareŽ in...@sferacarta.com

Disrupt07

unread,
Apr 3, 2007, 5:41:22 AM4/3/07
to sqlalchemy
@Glauco Thanks.

It worked for me with the set() function.

It would be nice if SQLAlchemy has something to constrain distinct
values on save() or flush().

Glauco

unread,
Apr 3, 2007, 5:48:35 AM4/3/07
to sqlal...@googlegroups.com
Disrupt07 ha scritto:

I think this is not work for SA....


if you have an unique field and do some
insert into table values ( 'boo');
you do an error and the DB do the unique check contraint for you....

in the same manner:

a = Table()
for x in ['boo','boo','boo']:
a.new( x );


i think is better let the DB do his work..... and SA remain the orm :-)

Eric Ongerth

unread,
May 24, 2007, 2:44:49 PM5/24/07
to sqlalchemy

On Apr 3, 2:41 am, "Disrupt07" <cvbn...@gmail.com> wrote:
> It would be nice if SQLAlchemy has something to constrain distinct
> values on save() or flush().

You might be interested in this:
http://www.sqlalchemy.org/trac/wiki/UsageRecipes/UniqueObject

Reply all
Reply to author
Forward
0 new messages