maxtepkeev / architect

A set of tools which enhances ORMs written in Python with more features
Other
391 stars 57 forks source link

Table has no partition for value 736094 #12

Closed clearclaw closed 9 years ago

clearclaw commented 9 years ago

DJango 1.7, MySQL (RDS) 5.6, latest Architect.

The model prefix:

@architect.install ("partition", type = "range", subtype = "date",
                    constraint = "month", column = "date")
class QEvent (TimeStampedModel):
 ...stuff...

./manage migrate run cleanly. New database. Empty table.

$ architect partition --module qeventlog.models
architect partition: result: successfully (re)configured the database for the following models: QEvent

What MySQL has to think about it:

mysql> create database qeventlog;
Query OK, 1 row affected (0.00 sec)

mysql> show create table qeventlog_qevent\G
ERROR 1046 (3D000): No database selected
mysql> use qeventlog;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show create table qeventlog_qevent\G
*************************** 1. row ***************************
       Table: qeventlog_qevent
Create Table: CREATE TABLE `qeventlog_qevent` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `created` datetime NOT NULL,
  `modified` datetime NOT NULL,
  `entity` varchar(64) NOT NULL,
  `source` varchar(64) NOT NULL,
  `timestamp` decimal(30,6) DEFAULT NULL,
  `keyname` varchar(64) NOT NULL,
  `value_num` decimal(30,6) DEFAULT NULL,
  `value_str` varchar(2048) DEFAULT NULL,
  PRIMARY KEY (`id`,`created`),
  KEY `qeventlog_qevent_c096cf48` (`entity`),
  KEY `qeventlog_qevent_a34b03a6` (`source`),
  KEY `qeventlog_qevent_d80b9c9a` (`timestamp`),
  KEY `qeventlog_qevent_6e313b5a` (`keyname`),
  KEY `qeventlog_qevent_101309c5` (`entity`,`source`,`timestamp`,`keyname`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
/*!50100 PARTITION BY RANGE (TO_DAYS(created))
(PARTITION qeventlog_qevent_y0000m00 VALUES LESS THAN (0) ENGINE = InnoDB) */
1 row in set (0.00 sec)

But on an attempted write into the table:

  File "/home/jcl/src/qeventlog/qeventlog/qetask.py", line 39, in record
    QEvent.bulk_import (record)
  File "/home/jcl/src/qeventlog/qeventlog/models.py", line 53, in bulk_import
    obj.save ()
  File "/usr/local/lib/python2.7/dist-packages/architect/orms/django/features.py", line 103, in wrapper
    if not partition.exists():
  File "/usr/local/lib/python2.7/dist-packages/architect/databases/mysql/partition.py", line 36, in exists
    """.format(parent_table=self.table, name=self._get_name()))
  File "/usr/local/lib/python2.7/dist-packages/architect/orms/django/features.py", line 47, in select_one
    self.execute(sql)
  File "/usr/local/lib/python2.7/dist-packages/architect/orms/django/features.py", line 44, in execute
    return self.connection.execute(sql)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python2.7/dist-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 63, in execute
    return self.cursor.execute(sql)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/mysql/base.py", line 128, in execute
    return self.cursor.execute(query, args)
  File "build/bdist.linux-x86_64/egg/MySQLdb/cursors.py", line 205, in execute
  File "build/bdist.linux-x86_64/egg/MySQLdb/connections.py", line 36, in defaulterrorhandler
InterfaceError: (0, '')

Suspect I'm doing something stupid?

clearclaw commented 9 years ago

Oh....Yes, I did a stupid.

maxtepkeev commented 9 years ago

Hi,

Glad you figured it out, but could you please elaborate a little bit more on this topic, what was the error on your side. That can probably help someone reading this issue in the future.

Thanks.

oriolj commented 8 years ago

Will help me now! It seems that I'm doing the same stupid thing... EDIT more info: It seems that for some reason I have no triggers on the target DB: SHOW TRIGGERS from enastatsng; Empty set (0.00 sec)

Is this normal?

maxtepkeev commented 8 years ago

@oriolj Can you please provide more info about your current setup. If you are on MySQL then it's normal that you don't have any triggers because MySQL partitioning implementation in Architect doesn't use triggers because MySQL doesn't allow dynamic SQL inside triggers/trigger functions. If you are on PostgreSQL then it's not normal that you don't have any triggers. Any way, please provide more info about your environment and steps you have done and I'll try to see where the error is. @clearclaw ping, if you remember what was the error on your side, we would appreciate your help. Thanks.

oriolj commented 8 years ago

I'm using MariaDB 10.0.21, here's my process:

create database enastatsng CHARACTER SET utf8; (grant permissions commands)

$ manage.py migrate $ export DJANGO_SETTINGS_MODULE=EnaStats.settings $ architect partition --module stats.models /home/oriol/venvs/enastats/lib/python2.7/site-packages/django/db/backends/mysql/base.py:124: Warning: Table 'mysql.index_stats' doesn't exist return self.cursor.execute(query, args) architect partition: result: successfully (re)configured the database for the following models: OriginalStatRecord

The OriginalStat record model: @architect.install('partition', type='range', subtype='date', constraint='month', column='orig_datetime_start') class OriginalStatRecord(models.Model): [...] orig_datetime_start = models.DateTimeField() [...]

Now if I go to mysql console and type:

use enastatsng; show create table stats_originalstatrecord; +--------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +--------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | stats_originalstatrecord | CREATE TABLE stats_originalstatrecord ( [...] generated_datetime_start datetime NOT NULL, [...] PRIMARY KEY (orig_datetime_start), ) ENGINE=InnoDB DEFAULT CHARSET=utf8 /!50100 PARTITION BY RANGE (TO_DAYS(orig_datetime_start)) (PARTITION stats_originalstatrecord_y0000m00 VALUES LESS THAN (0) ENGINE = InnoDB) / | +--------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)

Thanks!!

maxtepkeev commented 8 years ago

@oriolj Sorry for making you wait for so long, but I can't reproduce your problem here, but I have a theory. Are you trying to insert new objects in a bulk, e.g.:

Document.objects.bulk_create([
    Document(name='foo', created=datetime.datetime(2014, 4, 15, 18, 44, 23)),
    Document(name='bar', created=datetime.datetime(2014, 5, 15, 18, 44, 23)),
])

If yes, that won't work, because bulk_create method in Django doesn't call save() model method which Architect relies on for MySQL's table partitioning implementation. But the simple inserts like this

Document.objects.create(name='foo', created=datetime.datetime(2014, 4, 15, 18, 44, 23))
Document.objects.create(name='bar', created=datetime.datetime(2014, 5, 15, 18, 44, 23))

should work very well.

oriolj commented 8 years ago

@maxtepkeev you are totally right! Thanks!