need your help Mysql ERROR I tried but failed: - python

I am trying to run a app cloned from following repo
https://github.com/myogeshchavan97/fullstack_banking_app and stuck with mysql
I get following error when i run source /server/scripts.sql
I am using latest Server version: 8.0.23 Homebrew
ERROR 1064 (42000): You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near 'bank_account' at line 1
ERROR 1064 (42000): You
have an error in your SQL syntax; check the manual that corresponds
to your MySQL server version for the right syntax to use near
'BIGSERIAL PRIMARY KEY NOT NULL, first_name VARCHAR(32) NOT NULL,
last_name V' at line 2
ERROR 1064 (42000): You have an error in your
SQL syntax; check the manual that corresponds to your MySQL server
version for the right syntax to use near 'BIGSERIAL PRIMARY KEY NOT
NULL, access_token VARCHAR(500) NOT NULL, userid B' at line 2
scripts
CREATE DATABASE bank_account;
CREATE TABLE bank_user(
userid BIGSERIAL PRIMARY KEY NOT NULL,
first_name VARCHAR(32) NOT NULL,
last_name VARCHAR(32) NOT NULL,
email VARCHAR(32) NOT NULL,
password VARCHAR(255) NOT NULL,
unique(email)
);
CREATE TABLE TOKENS(
id BIGSERIAL PRIMARY KEY NOT NULL,
access_token VARCHAR(500) NOT NULL,
userid BIGSERIAL NOT NULL,
FOREIGN KEY(userid) REFERENCES bank_user(userid)
);
I get this error I tried with backticks as but unfortunately didn't work...I would appreciate your insight.

Try this:
CREATE TABLE bank_user( userid BIGINT PRIMARY KEY NOT NULL, first_name VARCHAR(32) NOT NULL, last_name VARCHAR(32) NOT NULL, email VARCHAR(32) NOT NULL, password VARCHAR(255) NOT NULL, unique(email) );
CREATE TABLE TOKENS( id BIGINT PRIMARY KEY NOT NULL, access_token VARCHAR(500) NOT NULL, userid BIGINT NOT NULL, FOREIGN KEY(userid) REFERENCES bank_user(userid) );
check the following link for mappings between PostgreSQL and MySQL.
In your case, instead of BIGSERIAL you need to use BIGINT.

The equivalent in MySQL would be:
CREATE TABLE bank_user (
userid BIGINT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(32) NOT NULL,
last_name VARCHAR(32) NOT NULL,
email VARCHAR(32) NOT NULL,
password VARCHAR(255) NOT NULL,
unique(email)
);
CREATE TABLE TOKENS (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
access_token VARCHAR(500) NOT NULL,
userid BIGINT NOT NULL,
FOREIGN KEY (userid) REFERENCES bank_user(userid)
);
Here is a db<>fiddle.
The equivalent of SERIAL is AUTO_INCREMENT.
Some comments:
32 characters may or may not be big enough for names. It is certainly not big enough for emails, which can get pretty long.
The password should be encrypted. Do not store clear-text passwords in the data.
I would be surprised if you really needed BIGINT for the userid. Are you really planning on having billions of users? A 4-byte integer should be sufficient.
NOT NULL is redundant when you declare a column to be the primary key.

Related

django 2.0 using MySQL database

I was curious about using MySQL instead of SQLite for my Django project. Firstly, I would like to ask: where can I find an explicite guide of how to install MySQL with Django on Windows and if I use MySQL, will I type the exact type of code I would usually type in SQLite ?
For example:
class Publisher(models.Model):
name = models.CharField(max_length=30)
address = models.CharField(max_length=50)
city = models.CharField(max_length=60)
state_province = models.CharField(max_length=30)
country = models.CharField(max_length=50)
website = models.URLField()
When you type this in Django, it gets converted to:
CREATE TABLE "books_publisher" (
"id" serial NOT NULL PRIMARY KEY,
"name" varchar(30) NOT NULL,
"address" varchar(50) NOT NULL,
"city" varchar(60) NOT NULL,
"state_province" varchar(30) NOT NULL,
"country" varchar(50) NOT NULL,
"website" varchar(200) NOT NULL
);
So the same will be when using MySQL ?

Set AUTOINCREMENT value in django table

I have the following table in mysql:
CREATE TABLE `portal_asset` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`asset_id` int(11) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=1000000 DEFAULT CHARSET=utf8;
How would I create thie same table in django? So far I have the following, but not sure how to set the AUTO_INCREMENT value --
class PortalAsset(models.Model):
id = models.IntegerField(primary_key=True)
asset_id = models.IntegerField(unique=True)
class Meta:
db_table = u'portal_asset'
How can I set the AUTO_INCREMENT value to start off at 1000000 ? The equivalent of:
alter table portal_asset AUTO_INCREMENT=1000000;
You can use a RunSQL operation in your migrations to execute the necessary SQL:
migrations.RunSQL("ALTER TABLE portal_asset AUTO_INCREMENT=1000000;")
If you haven't run any migrations, you can add this to your first migration to ensure no rows are inserted before the new value is set. Otherwise you'll have to add this operation in a new migration. You can create a new, empty migration using python manage.py makemigrations --empty <yourappname>.

Retrieving ForeignKey mapped objects in Python with SqlAlchemy

I have an existing database that I'm trying to map into SqlAlchemy's ORM. I want it to just figure out the ForiegnKey relations that already exist in the database itself.
Here's the code I have so far:
from sqlalchemy import create_engine, MetaData, Table
from sqlalchemy.orm import create_session
from sqlalchemy.ext.declarative import declarative_base
# connect to database and infer from structure
Base = declarative_base()
engine = create_engine("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
metadata = MetaData(bind=engine)
class Club(Base):
__table__ = Table('clubs', metadata, autoload=True)
def __repr__(self):
return "<Club: %s>" % (self.name,)
class Member(Base):
__table__ = Table('members', metadata, autoload=True)
def __repr__(self):
return "<Member: %s of %d>" % (self.name, self.club_id)
Here's the SQL table dump:
CREATE TABLE `clubs` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(45) collate utf8_bin default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
CREATE TABLE `members` (
`id` int(11) NOT NULL auto_increment,
`club_id` int(11) default NULL,
`name` VARCHAR(100) default NULL,
PRIMARY KEY (`id`),
KEY `fk_members_club_idx` (`club_id`),
CONSTRAINT `fk_members_club` FOREIGN KEY (`club_id`) REFERENCES `clubs` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
My problem is that in the Member __repr__ function, for example, instead of printing the club_id (which is useless to human), I'd like to print the Club name. Something like member.club.name would be ideal.
I couldn't find out how to do this on the SqlAlchemy docs unless I defined my own tables, not merely reflecting them in my code, which is what I'm doing here.
Just change your Member class to look like below:
class Member(Base):
__table__ = Table('members', metadata, autoload=True)
club = relationship(Club, backref="members")
def __repr__(self):
return "<Member: %s of %s>" % (self.name, self.club.name)
The point being that the reflection (autoload) will not automatically create relationships between classes, so you have to define them explicitly.

web2py reference field unique=True attribute not working

I working with web2py and have the following table definition:
my_info = db.define_table('my_info',
Field('my_info_id', 'reference other_info', requires=IS_IN_DB(db, other_info.id, ''), unique=True),
Field('interface', 'string', length=32, requires=[IS_NOT_EMPTY()]),
Field('size', 'integer', requires=[IS_NOT_EMPTY()]))
For some reason when I view the mysql create table syntax I do not see the UNIQUE_KEY field set. Here is the create table syntax:
CREATE TABLE `mgmt_info` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`my_info_id` int(11) DEFAULT NULL,
`interface` varchar(32) DEFAULT NULL,
`size` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `id__idx` (`id`),
CONSTRAINT `my_info_ibfk_1` FOREIGN KEY (`my_info_id`) REFERENCES `other_info` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8
Hope someone can let me know why I am not able to set a foreign key to unique and why it does not work.
Thanks,
nav
It is odd, but odder is that in your unique field you did put the IS_IN_DB validator. I do not know what you wanted to do, but you should replace by IS_NOT_IN_DB validator. Therefore, it will assure to you that your field 'my_info_id' is unique in your database.

Django - set ForeignKey deferrable foreign key constraint in SQLite3

I seem to be stuck with creating an initialy deferrable foreign key relationship between two models in Django and using SQLite3 as my backend storage.
Consider this simple example. This is what models.py looks like:
from django.db import models
class Investigator(models.Model):
name = models.CharField(max_length=250)
email = models.CharField(max_length=250)
class Project(models.Model):
name = models.CharField(max_length=250)
investigator = models.ForeignKey(Investigator)
And this is what the output from sqlall looks like:
BEGIN;
CREATE TABLE "moo_investigator" (
"id" integer NOT NULL PRIMARY KEY,
"name" varchar(250) NOT NULL,
"email" varchar(250) NOT NULL
)
;
CREATE TABLE "moo_project" (
"id" integer NOT NULL PRIMARY KEY,
"name" varchar(250) NOT NULL,
"investigator_id" integer NOT NULL REFERENCES "moo_investigator" ("id")
)
;
CREATE INDEX "moo_project_a7e50be7" ON "moo_project" ("investigator_id");
COMMIT;
"DEFERRABLE INITIALLY DEFERRED" is missing from the *investigator_id* column in the project table. What am I doing wrong?
p.s. I am new to Python and Django - using Python version 2.6.1 Django version 1.4 and SQLite version 3.6.12
This behavior is now the default. See https://github.com/django/django/blob/803840abf7dcb6ac190f021a971f1e3dc8f6792a/django/db/backends/sqlite3/schema.py#L16
Sqlite backend does not add "DEFERRABLE INITIALLY DEFERRED". Check the code

Categories