Skip to content

Commit

Permalink
[#1705] Update tag column unique constraint
Browse files Browse the repository at this point in the history
Tag names are no longer unique, instead (name, vocabulary_id) column pairs
in the tag table are unique. CKAN can now have two tags with the same
name, as long as they belong to different vocabularies. Each tag still
has a unique ID as its primary key.
  • Loading branch information
Sean Hammond committed Jan 30, 2012
1 parent ba1f50e commit 74ed1a2
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions ckan/model/tag.py
@@ -1,5 +1,4 @@
from sqlalchemy.orm import eagerload_all
from sqlalchemy import and_
import sqlalchemy
import vdm.sqlalchemy

from types import make_uuid
Expand All @@ -18,11 +17,11 @@

tag_table = Table('tag', metadata,
Column('id', types.UnicodeText, primary_key=True, default=make_uuid),
Column('name', types.Unicode(MAX_TAG_LENGTH), nullable=False,
unique=True),
Column('name', types.Unicode(MAX_TAG_LENGTH), nullable=False),
Column('vocabulary_id',
types.Unicode(vocabulary.VOCABULARY_NAME_MAX_LENGTH),
ForeignKey('vocabulary.id'))
ForeignKey('vocabulary.id')),
sqlalchemy.UniqueConstraint('name', 'vocabulary_id')
)

package_tag_table = Table('package_tag', metadata,
Expand All @@ -47,7 +46,7 @@ def delete(self):
def get(cls, reference):
'''Returns a tag object referenced by its id or name.'''
query = Session.query(cls).filter(cls.id==reference)
query = query.options(eagerload_all('package_tags'))
query = query.options(sqlalchemy.orm.eagerload_all('package_tags'))
tag = query.first()
if tag == None:
tag = cls.by_name(reference)
Expand All @@ -71,7 +70,7 @@ def search_by_name(cls, text_query):
def all(cls):
q = Session.query(cls)
q = q.distinct().join(PackageTagRevision)
q = q.filter(and_(
q = q.filter(sqlalchemy.and_(
PackageTagRevision.state == 'active', PackageTagRevision.current == True
))
return q
Expand All @@ -81,7 +80,7 @@ def packages_ordered(self):
q = Session.query(Package)
q = q.join(PackageTagRevision)
q = q.filter(PackageTagRevision.tag_id == self.id)
q = q.filter(and_(
q = q.filter(sqlalchemy.and_(
PackageTagRevision.state == 'active', PackageTagRevision.current == True
))
packages = [p for p in q]
Expand Down

0 comments on commit 74ed1a2

Please sign in to comment.