Skip to content

Commit

Permalink
Rename 'pk' field to '_pk'
Browse files Browse the repository at this point in the history
  • Loading branch information
kiy4h committed Nov 12, 2018
1 parent 3bc749a commit 62ed298
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
30 changes: 15 additions & 15 deletions statik/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,29 +232,29 @@ def load_model_data_item(self, item, model, seen_entries=None):
if not seen_entries:
seen_entries = set()

if not isinstance(item, dict) or 'pk' not in item:
if not isinstance(item, dict) or '_pk' not in item:
raise InvalidModelCollectionDataError(
model.name,
context=self.error_context
)

entry = StatikDatabaseInstance(
name=item['pk'],
name=item['_pk'],
from_dict=item,
model=model,
session=self.session,
encoding=self.encoding,
markdown_config=self.markdown_config
)
# duplicate primary key!
if entry.field_values['pk'] in seen_entries:
if entry.field_values['_pk'] in seen_entries:
raise DuplicateModelInstanceError(
model.name,
pk=entry.field_values['pk'],
pk=entry.field_values['_pk'],
context=self.error_context
)
else:
seen_entries.add(entry.field_values['pk'])
seen_entries.add(entry.field_values['_pk'])

self.load_model_implicit_data(entry)

Expand All @@ -269,7 +269,7 @@ def load_model_data_item(self, item, model, seen_entries=None):
except Exception as exc:
raise DataError(
model.name,
pk=entry.field_values['pk'],
pk=entry.field_values['_pk'],
message="failed to insert entry into in-memory database.",
orig_exc=exc,
context=self.error_context
Expand All @@ -296,14 +296,14 @@ def load_model_data_from_files(self, path, model):
error_context=self.error_context
)
# duplicate primary key!
if entry.field_values['pk'] in seen_entries:
if entry.field_values['_pk'] in seen_entries:
raise DuplicateModelInstanceError(
model.name,
pk=entry.field_values['pk'],
pk=entry.field_values['_pk'],
context=self.error_context
)
else:
seen_entries.add(entry.field_values['pk'])
seen_entries.add(entry.field_values['_pk'])

self.load_model_implicit_data(entry)

Expand All @@ -317,7 +317,7 @@ def load_model_data_from_files(self, path, model):
except Exception as exc:
raise DataError(
model.name,
pk=entry.field_values['pk'],
pk=entry.field_values['_pk'],
message="failed to insert entry into in-memory database.",
orig_exc=exc,
context=self.error_context
Expand Down Expand Up @@ -390,7 +390,7 @@ def __init__(self, model=None, session=None, **kwargs):

# convert the vars to their underscored representation
self.field_values = underscore_var_names(self.vars)
self.field_values['pk'] = self.name
self.field_values['_pk'] = self.name

# run through the foreign key fields to check their assignment
for field_name in self.model.field_names:
Expand Down Expand Up @@ -433,9 +433,9 @@ def __init__(self, model=None, session=None, **kwargs):
).one()
except NoResultFound:
# Only allow implicit tables if the model
# has no fields other than 'pk'
# has no fields other than '_pk'
if len(other_model.__table__._columns) == 1:
missing_items.append({'pk': item})
missing_items.append({'_pk': item})
else:
logger.warning('%s not found in %s' %
(item, other_model))
Expand Down Expand Up @@ -496,7 +496,7 @@ def get_or_create_association_table(model1_name, model2_name):
logger.debug("Generating model: %s", model.name)
model_fields = {
'__tablename__': model.name,
'pk': Column(String, primary_key=True)
'_pk': Column(String, primary_key=True)
}

# populate all of the relevant additional relationships for this model
Expand Down Expand Up @@ -537,7 +537,7 @@ def get_or_create_association_table(model1_name, model2_name):
back_populates = field.back_populates or 'children'
model_fields[back_populates] = relationship(
field.field_type,
backref=backref(field_name, remote_side=[model_fields['pk']])
backref=backref(field_name, remote_side=[model_fields['_pk']])
)
else:
kwargs = {}
Expand Down
8 changes: 4 additions & 4 deletions tests/modular/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ def test_database(self):
self.assertInstanceEqual({
'first_name': 'Michael',
'last_name': 'Anderson',
'pk': 'manderson',
'_pk': 'manderson',
'email': '[email protected]',
}, guests[0])
self.assertInstanceEqual({
'first_name': 'Gary',
'last_name': 'Merriweather',
'pk': 'gmerriweather',
'_pk': 'gmerriweather',
'email': '[email protected]',
}, guests[1])

Expand Down Expand Up @@ -108,12 +108,12 @@ def test_database(self):
bookings = db.session.query(Booking).order_by(Booking.pk).all()
self.assertEqual(2, len(bookings))
self.assertInstanceEqual({
'pk': '1',
'_pk': '1',
'guest': guests[1],
'room': guesthouse_rooms[1],
}, bookings[0])
self.assertInstanceEqual({
'pk': '2',
'_pk': '2',
'guest': guests[0],
'room': guesthouse_rooms[0],
}, bookings[1])
Expand Down

0 comments on commit 62ed298

Please sign in to comment.