Repository: googleapis/python-spanner-django

Issue:
django-test-suite: raw_query failure due to reference `book_id` a ForeignKey field that claims a non-existent column
Coming here from
```shell
GOOGLE_APPLICATION_CREDENTIALS=~/Downloads/appdev-soda-spanner-staging-b842e628aa41-orijtech.json
DJANGO_TEST_SUITE=raw_query ./django_test_suite.sh
```
we get a failure
```shell
Testing against Django installed in '/Users/emmanuelodeke/Desktop/spanner-orm/django_tests/django/django' with up to 8 processes
Importing application raw_query
Using existing test database for alias 'default' ('db-2019-12-05-100635')...
Operations to perform:
  Synchronize unmigrated apps: auth, contenttypes, messages, raw_query, sessions, staticfiles
  Apply all migrations: admin, sites
Synchronizing apps without migrations:
  Creating tables...
    Creating table django_content_type
    Creating table auth_permission
    Creating table auth_group
    Creating table auth_user
    Creating table django_session
    Creating table raw_query_author
    Creating table raw_query_book
    Creating table raw_query_bookfkaspk
Traceback (most recent call last):
  File "runtests.py", line 507, in <module>
    options.exclude_tags,
  File "runtests.py", line 294, in django_tests
    extra_tests=extra_tests,
  File "/Users/emmanuelodeke/Desktop/spanner-orm/django_tests/django/django/test/runner.py", line 629, in run_tests
    old_config = self.setup_databases(aliases=databases)
  File "/Users/emmanuelodeke/Desktop/spanner-orm/django_tests/django/django/test/runner.py", line 554, in setup_databases
    self.parallel, **kwargs
  File "/Users/emmanuelodeke/Desktop/spanner-orm/django_tests/django/django/test/utils.py", line 174, in setup_databases
    serialize=connection.settings_dict.get('TEST', {}).get('SERIALIZE', True),
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/spanner/django/creation.py", line 26, in create_test_db
    super().create_test_db(*args, **kwargs)
  File "/Users/emmanuelodeke/Desktop/spanner-orm/django_tests/django/django/db/backends/base/creation.py", line 72, in create_test_db
    run_syncdb=True,
  File "/Users/emmanuelodeke/Desktop/spanner-orm/django_tests/django/django/core/management/__init__.py", line 148, in call_command
    return command.execute(*args, **defaults)
  File "/Users/emmanuelodeke/Desktop/spanner-orm/django_tests/django/django/core/management/base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "/Users/emmanuelodeke/Desktop/spanner-orm/django_tests/django/django/core/management/base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "/Users/emmanuelodeke/Desktop/spanner-orm/django_tests/django/django/core/management/commands/migrate.py", line 203, in handle
    self.sync_apps(connection, executor.loader.unmigrated_apps)
  File "/Users/emmanuelodeke/Desktop/spanner-orm/django_tests/django/django/core/management/commands/migrate.py", line 337, in sync_apps
    editor.create_model(model)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/spanner/django/schema.py", line 73, in create_model
    self.execute(sql, params or None)
  File "/Users/emmanuelodeke/Desktop/spanner-orm/django_tests/django/django/db/backends/base/schema.py", line 137, in execute
    cursor.execute(sql, params)
  File "/Users/emmanuelodeke/Desktop/spanner-orm/django_tests/django/django/db/backends/utils.py", line 67, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/Users/emmanuelodeke/Desktop/spanner-orm/django_tests/django/django/db/backends/utils.py", line 76, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/Users/emmanuelodeke/Desktop/spanner-orm/django_tests/django/django/db/backends/utils.py", line 82, in _execute
    return self.cursor.execute(sql)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/spanner/dbapi/cursor.py", line 83, in execute
    self.__do_update_ddl(sql)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/spanner/dbapi/cursor.py", line 243, in __do_update_ddl
    return self.__db_handle.update_ddl(ddl_statements)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/spanner/dbapi/connection.py", line 68, in update_ddl
    return self.__dbhandle.update_ddl(ddl_statements).result()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/google/api_core/future/polling.py", line 127, in result
    raise self._exception
google.api_core.exceptions.GoogleAPICallError: None Table raw_query_bookfkaspk references nonexistent key column book_id.

Dropped db-2019-12-05-100635
```
but succinctly
```shell
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/spanner/dbapi/connection.py", line 68, in update_ddl
    return self.__dbhandle.update_ddl(ddl_statements).result()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/google/api_core/future/polling.py", line 127, in result
    raise self._exception
google.api_core.exceptions.GoogleAPICallError: None Table raw_query_bookfkaspk references nonexistent key column book_id.
```

and from the definition of the test's model https://github.com/timgraham/django/blob/e3b3534cb984431c878d0dc59c621a9c25630493/tests/raw_query/models.py#L26-L27
<img width="883" alt="Screen Shot 2019-12-05 at 10 14 00 AM" src="https://user-images.githubusercontent.com/4898263/70261763-f93f1800-1747-11ea-8b51-7f77ba4d073a.png">



Discussion:

Resolving patch:
diff --git a/spanner/django/schema.py b/spanner/django/schema.py
--- a/spanner/django/schema.py
+++ b/spanner/django/schema.py
@@ -63,7 +63,7 @@ def create_model(self, model):
         sql = self.sql_create_table % {
             "table": self.quote_name(model._meta.db_table),
             "definition": ", ".join(constraint for constraint in (*column_sqls, *constraints) if constraint),
-            "primary_key": model._meta.pk.attname,
+            "primary_key": model._meta.pk.column,
         }
         if model._meta.db_tablespace:
             tablespace_sql = self.connection.ops.tablespace_sql(model._meta.db_tablespace)


Test patch: