Skip to content

Commit

Permalink
Fixed adding optional json
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Tischenko authored and kozlovsky committed May 12, 2021
1 parent 2d0813d commit 64c3326
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
8 changes: 6 additions & 2 deletions pony/orm/migrations/dbschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ def exists(self, provider, connection, case_sensitive=True):

@classmethod
def from_attr(cls, table, attr):
from pony.orm.ormtypes import Json
schema = table.schema
if not attr.converters:
attr.converters = [schema.provider.get_converter_by_attr(attr)]
Expand All @@ -241,8 +242,11 @@ def from_attr(cls, table, attr):
col.nullable = attr.nullable or len(attr.entity.bases) != 0
col.auto = attr.auto
col.sql_default = attr.sql_default
if not attr.is_required and isinstance(attr.py_type, type) and issubclass(attr.py_type, basestring):
col.sql_default = ''
if not attr.is_required and isinstance(attr.py_type, type):
if issubclass(attr.py_type, basestring):
col.sql_default = ''
elif issubclass(attr.py_type, Json):
col.sql_default = '{}'
col.initial = attr.initial or attr.provided.initial
col.sql_type = attr.sql_type or converter.get_sql_type()
provided_cols = attr.provided.kwargs.get('columns', None)
Expand Down
44 changes: 44 additions & 0 deletions pony/orm/migrations/tests/test_schema_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -1312,6 +1312,50 @@ class NewStudent(Student):
self.assertEqual(len(migration.operations), 1) # ChangeDiscriminator
self.assertEqual(expected_schema, actual_schema)

def test_add_optional_json(self):
self.db2 = db2 = Database(**self.db_params)

class Department(db2.Entity):
number = PrimaryKey(int, auto=True)
name = Required(str, unique=True)
groups = Set("Group")
courses = Set("Course")

class Group(db2.Entity):
number = PrimaryKey(int)
major = Required(str, index=True)
dept = Required("Department", fk_name='dept_fk')
students = Set("Student")

class Course(db2.Entity):
name = Required(str, unique=True)
semester = Required(int)
lect_hours = Required(int, check='lect_hours > 0')
lab_hours = Required(int)
credits = Required(int, index=True)
dept = Required(Department)
students = Set("Student")
PrimaryKey(name, semester)
composite_index(lect_hours, lab_hours)

class Student(db2.Entity):
name = Required(str)
dob = Required(date)
tel = Optional(str)
picture = Optional(buffer, lazy=True)
gpa = Required(float, default=0)
group = Required(Group)
courses = Set(Course)
composite_key(name, dob)
complex_info = Optional(Json)

class NewStudent(Student):
avatar = Required(str)

expected_schema, actual_schema, migration = self.apply_migrate()
self.assertEqual(len(migration.operations), 1) # ChangeDiscriminator
self.assertEqual(expected_schema, actual_schema)


class TestPostgresMigrations(AbstractTestMigrations):
db_params = dict(provider='postgres', user='pony', password='pony', host='localhost', database='pony')
Expand Down

0 comments on commit 64c3326

Please sign in to comment.