Skip to content

Commit 0d89af2

Browse files
committed
unqiue metadata attr name per table
1 parent db41ecb commit 0d89af2

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

datajoint/declare.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
"NULL",
1717
} # SQL literals to be used without quotes (case insensitive)
1818
EXTERNAL_TABLE_ROOT = "~external"
19-
METADATA_ATTRIBUTES_SQL = ["`_timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP"]
19+
METADATA_ATTRIBUTES_SQL = [
20+
"`_{full_table_name}_timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP"
21+
]
22+
LONGEST_METADATA_ATTRIBUTE = len("__timestamp")
2023

2124
TYPE_PATTERN = {
2225
k: re.compile(v, re.I)
@@ -293,11 +296,14 @@ def declare(full_table_name, definition, context):
293296
:param context: dictionary of objects that might be referred to in the table
294297
:return: SQL CREATE TABLE statement, list of external stores used
295298
"""
296-
table_name = full_table_name.strip("`").split(".")[1]
297-
if len(table_name) > MAX_TABLE_NAME_LENGTH:
299+
if (
300+
len(full_table_name.replace("`", "")) + LONGEST_METADATA_ATTRIBUTE
301+
> MAX_TABLE_NAME_LENGTH
302+
):
298303
raise DataJointError(
299304
"Table name `{name}` exceeds the max length of {max_length}".format(
300-
name=table_name, max_length=MAX_TABLE_NAME_LENGTH
305+
name=full_table_name.replace("`", ""),
306+
max_length=MAX_TABLE_NAME_LENGTH - LONGEST_METADATA_ATTRIBUTE,
301307
)
302308
)
303309

@@ -309,7 +315,12 @@ def declare(full_table_name, definition, context):
309315
index_sql,
310316
external_stores,
311317
) = prepare_declare(definition, context)
312-
attribute_sql.extend(METADATA_ATTRIBUTES_SQL)
318+
attribute_sql.extend(
319+
[
320+
attr.format(full_table_name=full_table_name.replace("`", ""))
321+
for attr in METADATA_ATTRIBUTES_SQL
322+
]
323+
)
313324

314325
if not primary_key:
315326
raise DataJointError("Table must have a primary key")
@@ -412,7 +423,6 @@ def alter(definition, old_definition, context):
412423
index_sql,
413424
external_stores,
414425
) = prepare_declare(definition, context)
415-
attribute_sql.extend(METADATA_ATTRIBUTES_SQL)
416426
(
417427
table_comment_,
418428
primary_key_,
@@ -421,7 +431,6 @@ def alter(definition, old_definition, context):
421431
index_sql_,
422432
external_stores_,
423433
) = prepare_declare(old_definition, context)
424-
attribute_sql_.extend(METADATA_ATTRIBUTES_SQL)
425434

426435
# analyze differences between declarations
427436
sql = list()

tests_old/test_declare.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
raises,
77
assert_set_equal,
88
)
9+
10+
from tests_old.schema_simple import IJ, JI
911
from .schema import *
1012
import datajoint as dj
1113
import inspect
@@ -332,7 +334,7 @@ def test_long_table_name():
332334
"""
333335

334336
@schema
335-
class WhyWouldAnyoneCreateATableNameThisLong(dj.Manual):
337+
class WhyWouldAnyoneCreateATableNameThis(dj.Manual):
336338
definition = """
337339
master : int
338340
"""
@@ -341,3 +343,14 @@ class WithSuchALongPartNameThatItCrashesMySQL(dj.Part):
341343
definition = """
342344
-> (master)
343345
"""
346+
347+
@staticmethod
348+
def test_hidden_attributes():
349+
assert (
350+
list(JI().heading._attributes.keys())[-1]
351+
== "_djtest_relational.#j_i_timestamp"
352+
)
353+
assert (
354+
list(JI().heading.attributes.keys())[-1]
355+
!= "_djtest_relational.#j_i_timestamp"
356+
)

0 commit comments

Comments
 (0)