Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A relationship joined by date fails on form initialization. #91

Open
hitbox opened this issue Dec 14, 2015 · 0 comments
Open

A relationship joined by date fails on form initialization. #91

hitbox opened this issue Dec 14, 2015 · 0 comments

Comments

@hitbox
Copy link

hitbox commented Dec 14, 2015

The following fails using sqlalchemy.Date, works using a custom date field that accepts a single datetime.date object. Appears to stem from match_pk in utils.find_entity, assuming col.type.python_type takes one argument.
Any chance find_entity (here) could handle this without a custom type?
Sorry if this is too long, wasn't sure of a better way to explain. Comment out DateType = MyDateType to get TypeError exception.

#!/usr/bin/env python2
import datetime

import sqlalchemy as sa
from sqlalchemy import create_engine, types
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from wtforms_alchemy import ModelForm, ModelFormField, ModelFieldList

engine = create_engine('sqlite:///:memory:')
Base = declarative_base(engine)
Session = sessionmaker(bind=engine)
session = Session()

class MyDate(datetime.date):

    def __new__(cls, arg):
        if isinstance(arg, datetime.date):
            return arg
        return MyDate.__new__(cls, arg)

class MyDateType(types.TypeDecorator):
    impl = types.Date

    @property
    def python_type(self):
        return MyDate


# Gives the error
DateType = sa.Date

# No error
DateType = MyDateType

class Report(Base):
    __tablename__ = 'reports'

    date = sa.Column(DateType, primary_key=True)


class Section(Base):
    __tablename__ = 'subreports'

    date = sa.Column(DateType, sa.ForeignKey('reports.date'), primary_key=True)
    section = sa.Column(sa.Integer, primary_key=True)

    report = sa.orm.relationship('Report', backref='sections')


class SectionForm(ModelForm):
    class Meta:
        model = Section
        include_primary_keys = True
        include_foreign_keys = True


class ReportForm(ModelForm):
    class Meta:
        model = Report
        include_primary_keys = True
        include_foreign_keys = True

    sections = ModelFieldList(ModelFormField(SectionForm))


Base.metadata.create_all()
report = Report(date=datetime.date(2015,12,13),
                sections=[Section(section=1)])
session.add(report)
session.commit()

form = ReportForm(obj=report)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant