Pegase745 / sqlalchemy-datatables

SQLAlchemy integration of jQuery DataTables >= 1.10.x (Pyramid and Flask examples)
MIT License
161 stars 65 forks source link

postgresql schema name in sort_name #32

Closed luciman closed 8 years ago

luciman commented 8 years ago

Hi, I use the code in flask sqlalchemy postgresql, and my tables are in schemas

class User(db.Model, UserMixin):
    __tablename__ = 'user'
    __table_args__ = {'schema': 'usr'}

when i want to order by a column by clicking i will recive a postgresql error. in the postgresql select the order_by the sort_name has not schema name in front

Pegase745 commented 8 years ago

It works fine for me with this example. I just had to use the Model class name in the Foreign key instead of the string term.

class User(Base):

    """Define a User."""

    __tablename__ = 'users'
    __table_args__ = {'schema': 'main'}

    id = Column(Integer, primary_key=True)
    name = Column(Unicode, unique=True)
    created_at = Column(DateTime, default=datetime.datetime.utcnow)
    address = relationship('Address', uselist=False, backref=backref('user'))

    def __unicode__(self):
        """Give a readable representation of an instance."""
        return '%s' % self.name

    def __repr__(self):
        """Give a unambiguous representation of an instance."""
        return '<%s#%s>' % (self.__class__.__name__, self.id)

class Address(Base):

    """Define an Address."""

    __tablename__ = 'addresses'
    __table_args__ = {'schema': 'main'}

    id = Column(Integer, primary_key=True)
    description = Column(Unicode, unique=True)
    user_id = Column(Integer, ForeignKey(User.id))

    def __unicode__(self):
        """Give a readable representation of an instance."""
        return '%s' % (self.id)

    def __repr__(self):
        """Give a unambiguous representation of an instance."""
        return '<%s#%s>' % (self.__class__.__name__, self.id)
luciman commented 8 years ago

Thanks for answering. I'm using datatables just for the User table (without join) and it doesn't work.

In order to make it work I had to insert at line 321 the following:

            if hasattr(parent, '__table_args__'):
                schemaname = parent.__table_args__["schema"]
                tablename='%s.%s' % (schemaname, tablename)
Pegase745 commented 8 years ago

It would be nice if you could make a Pull Request with some tests or examples, please