How to import an existing database in my Django project? - python

I'm trying to add an existing sqlite3 database to my Django project but it doesn't seem to be recognized.
I've added that database to my project folder, modified the settings.py file to reflect that addition:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
},
'added_db': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'added_db.sqlite3'),
}
}
I then ran the 'python manage.py makemigrations' and the 'python manage.py migrate' commands but it doesn't seem to do anything. When I run 'python manage.py inspectdb', it only shows the tables within the default database.
What am I missing?

Depends on document:
The migrate management command operates on one database at a time. By default, it operates on the default database, but by providing the --database option, you can tell it to synchronize a different database.
So, you must provide your db name when you make migrate.For detail you can see here
./manage.py migrate --database=added_db

Related

Following Django tutorial, "python manage.py migrate" doesn't create any django tables (MySQL, Pycharm)

In mysite.settings.py I have:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': os.path.join(BASE_DIR, 'test'),
#'NAME': 'test',
'USER': 'root',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '3306',
}}
When I run 'python manage.py migrate' in the PyCharm terminal, nothing happens, no error message etc. No new tables are created in the database specified. I tried two things for NAME as you can see.
MySQL Server version 8.0.19
MySQL Connector/Python 8.0.19
Python version 3.8
In PyCharm I have package mysqlclient 1.4.6 installed.
Any advice on how to find the problem? How to get an error message?
Thank you
DATABASES property specifies your DB configurations. Now NAME is the property which specifies your database name.
Currently here 'NAME': os.path.join(BASE_DIR, 'test'), won't work as it will give you something like C:/YourUser/YourPath... so this string is not a valid db name. basically (os.path.join() returns a path to your project directory)
First create a db manually on MySql, note the name and put it in property as 'Name': DBNAME in SETTINGS.py.
(Make sure you have your environment set while running following commands)
Then try python manage.py makemigrations for checking migration changes, if everything goes fine up till here then fire python manage.py migrate command.

django settings.py won't read env variable for unit tests

I have database environment variables specified for my django app:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': os.getenv("POSTGRES_NAME"),
'USER': os.getenv("POSTGRES_USER"),
'PASSWORD': os.getenv("POSTGRES_PW"),
'HOST': os.getenv("POSTGRES_HOST"),
'PORT': os.getenv("POSTGRES_PORT"),
}
}
The variables successfully get read in when I run "python manage.py runserver", during the build on Circle CI, and also in its production environment. But I am not understanding why when I run unit tests they don't get read in.
Thanks for the help!
Turns out the problem was that I hadn't closed my IDE in some time. I had to reboot the IDE to source the env vars from the virtual environment.

Use PyCharm create a website project, but do not find the db.sqlite3 file

I use PyCharm create a website project, but do not find the db.sqlite3 file.
The database settings in settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
EDIT
You see the leftside bar there is no db.sqlite3 file, and I reveal in Finder still can not find it.
If there is no db files default, you should execute those command in PyCharm terminal:
python manage.py makemigrations
python manage.py migrate
Then you can see it.

Migrating development database onto Heroku with Django

I am having trouble migrating my development database onto Heroku. This is for a personal project written in Django.
settings.py
db_from_env = dj_database_url.config()
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
DATABASES['default'].update(db_from_env)
I've migrated the database locally and pushed the migration onto Heroku already. Then, I ran the heroku run python manage.py migrate command to migrate the database on Heroku.
I had accidentally ran makemigrations on Heroku, but have restarted my dynos since.
Currently, my database on Heroku is not populated at all.

Set up a Django Project with Mamp?

I just downloaded a newer version of MAMP (3.2.1) and i noticed that this Version has Python installed and also seems to handle SQLite Databases.
Shouldn't I be able to manage Django Projects with it?
Where and how would i install it?
I found some Posts in the Web (before my new MAMP release) where People already trying to get MAMP + Django to work with MySQL but those seemed more complicated to me then the usual setup with Virtualenv + SQLite/Postgres.
I'm pretty new to django but starting a project at the time seems quite simple to me.
If Django would work with MAMP together what would be the advantages?
Anyone has already experiences or useful links?
OK i gues working with MAMP MySQL has the advantage that i can easy import/export Database with php MyAdmin tool.
Anyway based on tanorix answer here how for me Django worked with MAMP MySQL Database:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'projectdb',
'USER': 'root',
'PASSWORD': 'root',
'HOST': '/Applications/MAMP/tmp/mysql/mysql.sock',
'PORT': '8888',
}
}
Then
python manage.py migrate
I don't have knowledge about MAMP but I can give you some elements to put Django Database with WAMP, so I think it can be the same manipulation:
First, in MAMP, you need to create a database, call it : projectdb.
Then, at your settings.py, update your variable DATABASES like this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'projectdb', # Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
'USER': 'root',
'PASSWORD': '',
'HOST': '127.0.0.1', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
'PORT': '', # Set to empty string for default.
}
}
Then, if you are using South, at your shell write this:
python manage.py schemamigration <name of your app> --init
python manage.py syncdb # => create your tables at your MAMP
python manage.py migrate

Categories

Resources