Re: drop/createTables

24 views
Skip to first unread message

Jeremy Lainé

unread,
Jul 18, 2014, 3:48:16 AM7/18/14
to otop...@gmail.com, qdj...@googlegroups.com

Hi,

Your point is completely valid, please open an issue on github to track it.

Jeremy

On 17 Jul 2014 13:12, otop...@gmail.com wrote:
I am using: qdjango v0.4.0-158-g810b021, mysql 5.5.37 on 64bit Linux

I am having problems creating/droping tables from database with functions QDjango::createTables(); and QDjango::dropTables();
It seems that those functions create tables in alphabetical order - it does not care for FK dependencies.

If I have table A which has foreign key to table B:

SQL query "CREATE TABLE `A` (`id` integer `B_id` integer NOT NULL, CONSTRAINT `FK_B_id` FOREIGN KEY (`B_id`) REFERENCES `B` (`id`) ON DELETE RESTRICT)"


Then it ends with error like

SQL error QSqlError("1005", "QMYSQL: Unable to execute query", "Can't create table 'DB.A' (errno: 150)")


There is workaround - to run createTables many times (or until there is no error 150).

It would be nice if the createTables() checked for foreign keys first,
from these FK dependencies created list of tables in right order and then created those tables.

--
You received this message because you are subscribed to the Google Groups "QDjango" group.
To unsubscribe from this group and stop receiving emails from it, send an email to qdjango+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

otop...@gmail.com

unread,
Jul 18, 2014, 4:35:31 AM7/18/14
to qdj...@googlegroups.com, otop...@gmail.com
Is there a way to create/drop just one table?

Jeremy Lainé

unread,
Jul 18, 2014, 4:48:49 AM7/18/14
to qdj...@googlegroups.com
On 07/18/2014 10:35 AM, otop...@gmail.com wrote:
> Is there a way to create/drop just one table?
>

Yes absolutely:

QDjangoMetaModel metaModel = QDjango::registerModel<SomeModel>();
metaModel.createTable()
metaModel.dropTable()


Jeremy

otop...@gmail.com

unread,
Jul 18, 2014, 9:00:23 AM7/18/14
to qdj...@googlegroups.com
Ok I found better solution for my problem.
I've modified createTables and dropTables to create and drop them in order of registration.
So I register tables in correct order and then they are created or dropped without error :)

In QDjango.cpp added global list of table names:

QList<QByteArray> globalModelOrder = QList<QByteArray>();

These are modified functions, dropTables must iterate in reverse order..

bool QDjango::createTables()
{
   
bool ret = true;
   
foreach (const QByteArray &name, globalModelOrder)
       
if (!globalMetaModels[name].createTable())
            ret
= false;
   
return ret;
}

bool QDjango::dropTables()
{
   
bool ret = true;
   
QList<QByteArray>::iterator it = globalModelOrder.end();
   
while (it != globalModelOrder.begin()) {
       
--it;
       
if (!globalMetaModels[*it].dropTable())
            ret
= false;
   
}
   
return ret;
}

otop...@gmail.com

unread,
Jul 18, 2014, 9:02:48 AM7/18/14
to qdj...@googlegroups.com
I forgot this is modified registerModel:

QDjangoMetaModel QDjango::registerModel(const QMetaObject *meta)
{
   
const QByteArray name = meta->className();
   
if (!globalMetaModels.contains(name))
   
{
        globalMetaModels
.insert(name, QDjangoMetaModel(meta));
        globalModelOrder
.append(name);
   
}
   
return globalMetaModels[name];
}

Jeremy Lainé

unread,
Jul 18, 2014, 12:41:48 PM7/18/14
to otop...@gmail.com, qdj...@googlegroups.com

This is not really a "better solution" since it requires the user to register his models in a specific order..

--
Reply all
Reply to author
Forward
0 new messages