Fixes in webmodel for delete rows in tables with constraints and tests
This commit is contained in:
parent
16fa9b550a
commit
3dc59a3fe3
3 changed files with 36 additions and 2 deletions
|
|
@ -27,6 +27,7 @@ def get_language(s):
|
|||
lang_selected=s['lang']
|
||||
else:
|
||||
s['lang']=I18n.default_lang
|
||||
s.save()
|
||||
lang_selected=I18n.default_lang
|
||||
|
||||
return lang_selected
|
||||
|
|
|
|||
|
|
@ -627,7 +627,7 @@ class WebModel:
|
|||
|
||||
id_table_related=self.fields[field].table_id
|
||||
|
||||
self.arr_sql_set_index[self.name][field]='ALTER TABLE `'+self.name+'` ADD CONSTRAINT `'+field+'_'+self.name+'IDX` FOREIGN KEY ( `'+field+'` ) REFERENCES `'+table_related+'` (`'+id_table_related+'`) ON DELETE RESTRICT ON UPDATE RESTRICT;'
|
||||
self.arr_sql_set_index[self.name][field]='ALTER TABLE `'+self.name+'` ADD CONSTRAINT `'+field+'_'+self.name+'IDX` FOREIGN KEY ( `'+field+'` ) REFERENCES `'+table_related+'` (`'+id_table_related+'`) ON DELETE CASCADE ON UPDATE CASCADE;'
|
||||
|
||||
return "create table `"+self.name+"` (\n"+",\n".join(table_fields)+"\n) DEFAULT CHARSET=utf8;";
|
||||
|
||||
|
|
@ -682,7 +682,7 @@ class WebModel:
|
|||
|
||||
id_table_related=self.fields[field].table_id
|
||||
|
||||
self.query('ALTER TABLE `'+self.name+'` ADD CONSTRAINT `'+field+'_'+self.name+'IDX` FOREIGN KEY ( `'+field+'` ) REFERENCES `'+table_related+'` (`'+id_table_related+'`) ON DELETE RESTRICT ON UPDATE RESTRICT;', [], self.connection_id)
|
||||
self.query('ALTER TABLE `'+self.name+'` ADD CONSTRAINT `'+field+'_'+self.name+'IDX` FOREIGN KEY ( `'+field+'` ) REFERENCES `'+table_related+'` (`'+id_table_related+'`) ON DELETE CASCADE ON UPDATE CASCADE;', [], self.connection_id)
|
||||
|
||||
for field in fields_to_add_unique:
|
||||
|
||||
|
|
|
|||
|
|
@ -15,6 +15,17 @@ class ExampleModel(WebModel):
|
|||
self.register(corefields.CharField('title'))
|
||||
self.register(corefields.CharField('content'))
|
||||
|
||||
class ForeignKeyExampleModel(WebModel):
|
||||
|
||||
def __init__(self, connection):
|
||||
|
||||
super().__init__(connection)
|
||||
|
||||
# I can change other fields here, how the name.
|
||||
|
||||
self.register(corefields.ForeignKeyField('example_id', ExampleModel(connection), size=11, required=False, identifier_field='id', named_field="id", select_fields=[]))
|
||||
|
||||
|
||||
class ExampleModel2(WebModel):
|
||||
|
||||
def __init__(self, connection):
|
||||
|
|
@ -151,6 +162,28 @@ class TestWebModelMethods(unittest.TestCase):
|
|||
|
||||
connection.close()
|
||||
|
||||
def test_zcheck_1_foreignkeys(self):
|
||||
|
||||
connection=WebModel.connection()
|
||||
model=ExampleModel(connection)
|
||||
foreignkey=ForeignKeyExampleModel(connection)
|
||||
|
||||
print('Checking ForeignKeys...')
|
||||
|
||||
sql=foreignkey.create_table()
|
||||
|
||||
print('Creating foreignkey table...')
|
||||
|
||||
self.assertTrue(foreignkey.query(sql))
|
||||
|
||||
|
||||
|
||||
print('Dropping foreignkey table...')
|
||||
|
||||
self.assertTrue(foreignkey.drop())
|
||||
|
||||
pass
|
||||
|
||||
def test_zcheck_connections(self):
|
||||
|
||||
print('Check connection of models...')
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue